class EventOnFailureTest

Public Instance Methods

invalidate(object, attribute, message, values = []) click to toggle source
# File test/unit/event_test.rb, line 893
def invalidate(object, attribute, message, values = [])
  (object.errors ||= []) << generate_message(message, values)
end
reset(object) click to toggle source
# File test/unit/event_test.rb, line 897
def reset(object)
  object.errors = []
end
setup() click to toggle source
# File test/unit/event_test.rb, line 889
def setup
  StateMachine::Integrations.const_set('Custom', Module.new do
    include StateMachine::Integrations::Base
    
    def invalidate(object, attribute, message, values = [])
      (object.errors ||= []) << generate_message(message, values)
    end
    
    def reset(object)
      object.errors = []
    end
  end)
  
  @klass = Class.new do
    attr_accessor :errors
  end
  
  @machine = StateMachine::Machine.new(@klass, :integration => :custom)
  @machine.state :parked
  @machine.events << @event = StateMachine::Event.new(@machine, :ignite)
  
  @object = @klass.new
  @object.state = 'parked'
end
teardown() click to toggle source
# File test/unit/event_test.rb, line 935
def teardown
  StateMachine::Integrations.send(:remove_const, 'Custom')
end
test_should_invalidate_the_state() click to toggle source
# File test/unit/event_test.rb, line 914
def test_should_invalidate_the_state
  @event.fire(@object)
  assert_equal ['cannot transition via "ignite"'], @object.errors
end
test_should_run_failure_callbacks() click to toggle source
# File test/unit/event_test.rb, line 919
def test_should_run_failure_callbacks
  callback_args = nil
  @machine.after_failure {|*args| callback_args = args}
  
  @event.fire(@object)
  
  object, transition = callback_args
  assert_equal @object, object
  assert_not_nil transition
  assert_equal @object, transition.object
  assert_equal @machine, transition.machine
  assert_equal :ignite, transition.event
  assert_equal :parked, transition.from_name
  assert_equal :parked, transition.to_name
end