Skip to content

Instantly share code, notes, and snippets.

@chrisroos
Created May 5, 2015 09:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisroos/e212474311d1b93820e6 to your computer and use it in GitHub Desktop.
Save chrisroos/e212474311d1b93820e6 to your computer and use it in GitHub Desktop.
Illustration of the problem of precalculation blocks not being evaluated in the first question

Demonstration of the problem of precalculate blocks not being evaluated in the first question of a Smart Answer.

Assuming the flow lives in "lib/smart_answer_flows/" and the test lives in "test/integration/smart_answer_flows/", you can run the test with bundle exec ruby test/integration/smart_answer_flows/example_flow_test.rb.

Note the absence of "Q1 - precalculate" in output.txt.

# encoding: UTF-8
require_relative '../../test_helper'
require_relative 'flow_test_helper'
class ExampleFlowTest < ActiveSupport::TestCase
include FlowTestHelper
setup do
setup_for_testing_flow 'example-flow'
end
should "be on question 1" do
assert_current_node :question_1
end
context "when answering question 1" do
setup do
add_response :A
end
should "be on question 2" do
assert_current_node :question_2
end
context "when answering question 2" do
setup do
add_response :C
end
should "be on outcome 1" do
assert_current_node :outcome_1
end
end
end
end
multiple_choice :question_1 do
option :A
option :B
precalculate :precalculated_answer do
p '## Q1 - precalculate'
end
next_node do |response|
p '## Q1 - next_node'
:question_2
end
end
multiple_choice :question_2 do
option :C
option :D
precalculate :q2_precalculated_value do
p '## Q2 - precalculate'
end
next_node do |response|
p '## Q2 - next_node'
:outcome_1
end
end
outcome :outcome_1 do
end
$ bundle exec ruby test/integration/smart_answer_flows/example_flow_test.rb
Run options: --seed 57661
# Running:
."## Q1 - next_node"
"## Q2 - precalculate"
."## Q1 - next_node"
"## Q2 - precalculate"
"## Q2 - next_node"
.
Finished in 0.298484s, 10.0508 runs/s, 30.1524 assertions/s.
3 runs, 9 assertions, 0 failures, 0 errors, 0 skips
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment