class MongoMapperTest::MachineWithCallbacksTest

Public Instance Methods

setup() click to toggle source
# File test/unit/integrations/mongo_mapper_test.rb, line 683
def setup
  @model = new_model
  @machine = StateMachine::Machine.new(@model, :initial => :parked)
  @machine.other_states :idling
  @machine.event :ignite
  
  @record = @model.new(:state => 'parked')
  @transition = StateMachine::Transition.new(@record, @machine, :ignite, :parked, :idling)
end
test_should_allow_string_callbacks() click to toggle source
# File test/unit/integrations/mongo_mapper_test.rb, line 803
def test_should_allow_string_callbacks
  class << @record
    attr_reader :callback_result
  end
  
  @machine.before_transition('@callback_result = [1, 2, 3]')
  @transition.perform
  
  assert_equal [1, 2, 3], @record.callback_result
end
test_should_allow_symbolic_callbacks() click to toggle source
# File test/unit/integrations/mongo_mapper_test.rb, line 789
def test_should_allow_symbolic_callbacks
  callback_args = nil
  
  klass = class << @record; self; end
  klass.send(:define_method, :after_ignite) do |*args|
    callback_args = args
  end
  
  @machine.before_transition(:after_ignite)
  
  @transition.perform
  assert_equal [@transition], callback_args
end
test_should_include_transition_states_in_known_states() click to toggle source
# File test/unit/integrations/mongo_mapper_test.rb, line 783
def test_should_include_transition_states_in_known_states
  @machine.before_transition :to => :first_gear, :do => lambda {}
  
  assert_equal [:parked, :idling, :first_gear], @machine.states.map {|state| state.name}
end
test_should_pass_record_and_transition_to_after_callbacks_with_multiple_arguments() click to toggle source
# File test/unit/integrations/mongo_mapper_test.rb, line 741
def test_should_pass_record_and_transition_to_after_callbacks_with_multiple_arguments
  callback_args = nil
  @machine.after_transition {|*args| callback_args = args}
  
  @transition.perform
  assert_equal [@record, @transition], callback_args
end
test_should_pass_record_and_transition_to_before_callbacks_with_multiple_arguments() click to toggle source
# File test/unit/integrations/mongo_mapper_test.rb, line 709
def test_should_pass_record_and_transition_to_before_callbacks_with_multiple_arguments
  callback_args = nil
  @machine.before_transition {|*args| callback_args = args}
  
  @transition.perform
  assert_equal [@record, @transition], callback_args
end
test_should_pass_record_to_after_callbacks_with_one_argument() click to toggle source
# File test/unit/integrations/mongo_mapper_test.rb, line 733
def test_should_pass_record_to_after_callbacks_with_one_argument
  record = nil
  @machine.after_transition {|arg| record = arg}
  
  @transition.perform
  assert_equal @record, record
end
test_should_pass_record_to_before_callbacks_with_one_argument() click to toggle source
# File test/unit/integrations/mongo_mapper_test.rb, line 701
def test_should_pass_record_to_before_callbacks_with_one_argument
  record = nil
  @machine.before_transition {|arg| record = arg}
  
  @transition.perform
  assert_equal @record, record
end
test_should_run_after_callbacks() click to toggle source
# File test/unit/integrations/mongo_mapper_test.rb, line 725
def test_should_run_after_callbacks
  called = false
  @machine.after_transition {called = true}
  
  @transition.perform
  assert called
end
test_should_run_after_callbacks_if_model_callback_added_prior_to_state_machine_definition() click to toggle source
# File test/unit/integrations/mongo_mapper_test.rb, line 757
def test_should_run_after_callbacks_if_model_callback_added_prior_to_state_machine_definition
  model = new_model do
    after_save { nil }
  end
  machine = StateMachine::Machine.new(model, :initial => :parked)
  machine.other_states :idling
  machine.event :ignite
  after_called = false
  machine.after_transition {after_called = true}
  
  record = model.new(:state => 'parked')
  transition = StateMachine::Transition.new(record, machine, :ignite, :parked, :idling)
  transition.perform
  assert_equal true, after_called
end
test_should_run_after_callbacks_outside_the_context_of_the_record() click to toggle source
# File test/unit/integrations/mongo_mapper_test.rb, line 749
def test_should_run_after_callbacks_outside_the_context_of_the_record
  context = nil
  @machine.after_transition {context = self}
  
  @transition.perform
  assert_equal self, context
end
test_should_run_around_callbacks() click to toggle source
# File test/unit/integrations/mongo_mapper_test.rb, line 773
def test_should_run_around_callbacks
  before_called = false
  after_called = false
  @machine.around_transition {|block| before_called = true; block.call; after_called = true}
  
  @transition.perform
  assert before_called
  assert after_called
end
test_should_run_before_callbacks() click to toggle source
# File test/unit/integrations/mongo_mapper_test.rb, line 693
def test_should_run_before_callbacks
  called = false
  @machine.before_transition {called = true}
  
  @transition.perform
  assert called
end
test_should_run_before_callbacks_outside_the_context_of_the_record() click to toggle source
# File test/unit/integrations/mongo_mapper_test.rb, line 717
def test_should_run_before_callbacks_outside_the_context_of_the_record
  context = nil
  @machine.before_transition {context = self}
  
  @transition.perform
  assert_equal self, context
end