class EventCollectionWithValidationsTest

Public Class Methods

new() click to toggle source
Calls superclass method
# File test/unit/event_collection_test.rb, line 281
def initialize
  @errors = []
  super
end

Public Instance Methods

invalidate(object, attribute, message, values = []) click to toggle source
# File test/unit/event_collection_test.rb, line 269
def invalidate(object, attribute, message, values = [])
  (object.errors ||= []) << generate_message(message, values)
end
reset(object) click to toggle source
# File test/unit/event_collection_test.rb, line 273
def reset(object)
  object.errors = []
end
setup() click to toggle source
# File test/unit/event_collection_test.rb, line 265
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
    
    def initialize
      @errors = []
      super
    end
  end
  
  @machine = StateMachine::Machine.new(@klass, :initial => :parked, :action => :save, :integration => :custom)
  @events = StateMachine::EventCollection.new(@machine)
  
  @parked, @idling = @machine.state :parked, :idling
  @events << @ignite = StateMachine::Event.new(@machine, :ignite)
  @machine.events.concat(@events)
  
  @object = @klass.new
end
teardown() click to toggle source
# File test/unit/event_collection_test.rb, line 329
def teardown
  StateMachine::Integrations.send(:remove_const, 'Custom')
end
test_should_invalidate_if_event_cannot_be_fired() click to toggle source
# File test/unit/event_collection_test.rb, line 304
def test_should_invalidate_if_event_cannot_be_fired
  @object.state = 'idling'
  @object.state_event = 'ignite'
  @events.attribute_transition_for(@object, true)
  
  assert_equal ['cannot transition when idling'], @object.errors
end
test_should_invalidate_if_invalid_event_specified() click to toggle source
# File test/unit/event_collection_test.rb, line 297
def test_should_invalidate_if_invalid_event_specified
  @object.state_event = 'invalid'
  @events.attribute_transition_for(@object, true)
  
  assert_equal ['is invalid'], @object.errors
end
test_should_invalidate_with_human_name_if_invalid_event_specified() click to toggle source
# File test/unit/event_collection_test.rb, line 312
def test_should_invalidate_with_human_name_if_invalid_event_specified
  @idling.human_name = 'waiting'
  @object.state = 'idling'
  @object.state_event = 'ignite'
  @events.attribute_transition_for(@object, true)
  
  assert_equal ['cannot transition when waiting'], @object.errors
end
test_should_not_invalidate_event_can_be_fired() click to toggle source
# File test/unit/event_collection_test.rb, line 321
def test_should_not_invalidate_event_can_be_fired
  @ignite.transition :parked => :idling
  @object.state_event = 'ignite'
  @events.attribute_transition_for(@object, true)
  
  assert_equal [], @object.errors
end