class MachineCollectionFireWithValidationsTest
Public Class Methods
new()
click to toggle source
Calls superclass method
# File test/unit/machine_collection_test.rb, line 280 def initialize @errors = [] super end
Public Instance Methods
invalidate(object, attribute, message, values = [])
click to toggle source
# File test/unit/machine_collection_test.rb, line 268 def invalidate(object, attribute, message, values = []) (object.errors ||= []) << generate_message(message, values) end
reset(object)
click to toggle source
# File test/unit/machine_collection_test.rb, line 272 def reset(object) object.errors = [] end
setup()
click to toggle source
# File test/unit/machine_collection_test.rb, line 264 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 @machines = StateMachine::MachineCollection.new @machines[:state] = @state = StateMachine::Machine.new(@klass, :state, :initial => :parked, :integration => :custom) @state.event :ignite do transition :parked => :idling end @machines[:alarm_state] = @alarm_state = StateMachine::Machine.new(@klass, :alarm_state, :initial => :active, :namespace => 'alarm', :integration => :custom) @alarm_state.event :disable do transition :active => :off end @object = @klass.new end
teardown()
click to toggle source
# File test/unit/machine_collection_test.rb, line 325 def teardown StateMachine::Integrations.send(:remove_const, 'Custom') end
test_should_invalidate_if_no_transitions_exist()
click to toggle source
# File test/unit/machine_collection_test.rb, line 305 def test_should_invalidate_if_no_transitions_exist @object.state = 'idling' @object.alarm_state = 'off' assert !@machines.fire_events(@object, :ignite, :disable_alarm) assert_equal ['cannot transition via "ignite"', 'cannot transition via "disable"'], @object.errors end
test_should_not_invalidate_if_transitions_exist()
click to toggle source
# File test/unit/machine_collection_test.rb, line 300 def test_should_not_invalidate_if_transitions_exist assert @machines.fire_events(@object, :ignite, :disable_alarm) assert_equal [], @object.errors end
test_should_run_failure_callbacks_if_no_transitions_exist()
click to toggle source
# File test/unit/machine_collection_test.rb, line 313 def test_should_run_failure_callbacks_if_no_transitions_exist @object.state = 'idling' @object.alarm_state = 'off' @machines[:state].after_failure {@state_failure_run = true} @machines[:alarm_state].after_failure {@alarm_state_failure_run = true} assert !@machines.fire_events(@object, :ignite, :disable_alarm) assert @state_failure_run assert @alarm_state_failure_run end