class DataMapperTest::MachineWithCallbacksTest

Public Instance Methods

setup() click to toggle source
# File test/unit/integrations/data_mapper_test.rb, line 786
def setup
  @resource = new_resource
  @machine = StateMachine::Machine.new(@resource)
  @machine.state :parked, :idling
  @machine.event :ignite
  
  @record = @resource.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/data_mapper_test.rb, line 884
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/data_mapper_test.rb, line 870
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_pass_transition_to_after_callbacks_with_multiple_arguments() click to toggle source
# File test/unit/integrations/data_mapper_test.rb, line 836
def test_should_pass_transition_to_after_callbacks_with_multiple_arguments
  callback_args = nil
  @machine.after_transition {|*args| callback_args = args}
  
  @transition.perform
  assert_equal [@transition], callback_args
end
test_should_pass_transition_to_before_callbacks_with_multiple_arguments() click to toggle source
# File test/unit/integrations/data_mapper_test.rb, line 812
def test_should_pass_transition_to_before_callbacks_with_multiple_arguments
  callback_args = nil
  @machine.before_transition {|*args| callback_args = args}
  
  @transition.perform
  assert_equal [@transition], callback_args
end
test_should_pass_transition_to_before_callbacks_with_one_argument() click to toggle source
# File test/unit/integrations/data_mapper_test.rb, line 804
def test_should_pass_transition_to_before_callbacks_with_one_argument
  transition = nil
  @machine.before_transition {|arg| transition = arg}
  
  @transition.perform
  assert_equal @transition, transition
end
test_should_run_after_callbacks() click to toggle source
# File test/unit/integrations/data_mapper_test.rb, line 828
def test_should_run_after_callbacks
  called = false
  @machine.after_transition {called = true}
  
  @transition.perform
  assert called
end
test_should_run_after_callbacks_with_the_context_of_the_record() click to toggle source
# File test/unit/integrations/data_mapper_test.rb, line 844
def test_should_run_after_callbacks_with_the_context_of_the_record
  context = nil
  @machine.after_transition {context = self}
  
  @transition.perform
  assert_equal @record, context
end
test_should_run_around_callbacks() click to toggle source
# File test/unit/integrations/data_mapper_test.rb, line 852
def test_should_run_around_callbacks
  before_called = false
  after_called = [false]
  @machine.around_transition {|block| before_called = true; block.call; after_called[0] = true}
  
  @transition.perform
  assert before_called
  assert after_called[0]
end
test_should_run_around_callbacks_with_the_context_of_the_record() click to toggle source
# File test/unit/integrations/data_mapper_test.rb, line 862
def test_should_run_around_callbacks_with_the_context_of_the_record
  context = nil
  @machine.around_transition {|block| context = self; block.call}
  
  @transition.perform
  assert_equal @record, context
end
test_should_run_before_callbacks() click to toggle source
# File test/unit/integrations/data_mapper_test.rb, line 796
def test_should_run_before_callbacks
  called = false
  @machine.before_transition {called = true}
  
  @transition.perform
  assert called
end
test_should_run_before_callbacks_within_the_context_of_the_record() click to toggle source
# File test/unit/integrations/data_mapper_test.rb, line 820
def test_should_run_before_callbacks_within_the_context_of_the_record
  context = nil
  @machine.before_transition {context = self}
  
  @transition.perform
  assert_equal @record, context
end