class MachineCollectionFireTest

Public Instance Methods

save() click to toggle source
# File test/unit/machine_collection_test.rb, line 120
def save
  @saved = true
end
setup() click to toggle source
# File test/unit/machine_collection_test.rb, line 114
def setup
  @machines = StateMachine::MachineCollection.new
  
  @klass = Class.new do
    attr_reader :saved
    
    def save
      @saved = true
    end
  end
  
  # First machine
  @machines[:state] = @state = StateMachine::Machine.new(@klass, :state, :initial => :parked, :action => :save)
  @state.event :ignite do
    transition :parked => :idling
  end
  @state.event :park do
    transition :idling => :parked
  end
  
  # Second machine
  @machines[:alarm_state] = @alarm_state = StateMachine::Machine.new(@klass, :alarm_state, :initial => :active, :action => :save, :namespace => 'alarm')
  @alarm_state.event :enable do
    transition :off => :active
  end
  @alarm_state.event :disable do
    transition :active => :off
  end
  
  @object = @klass.new
end
test_should_be_successful_if_all_events_transition() click to toggle source
# File test/unit/machine_collection_test.rb, line 175
def test_should_be_successful_if_all_events_transition
  assert @machines.fire_events(@object, :ignite, :disable_alarm)
  assert_equal 'idling', @object.state
  assert_equal 'off', @object.alarm_state
  assert @object.saved
end
test_should_fail_if_any_event_cannot_transition() click to toggle source
# File test/unit/machine_collection_test.rb, line 154
def test_should_fail_if_any_event_cannot_transition
  assert !@machines.fire_events(@object, :park, :disable_alarm)
  assert_equal 'parked', @object.state
  assert_equal 'active', @object.alarm_state
  assert !@object.saved
  
  assert !@machines.fire_events(@object, :ignite, :enable_alarm)
  assert_equal 'parked', @object.state
  assert_equal 'active', @object.alarm_state
  assert !@object.saved
end
test_should_not_save_if_skipping_action() click to toggle source
# File test/unit/machine_collection_test.rb, line 182
def test_should_not_save_if_skipping_action
  assert @machines.fire_events(@object, :ignite, :disable_alarm, false)
  assert_equal 'idling', @object.state
  assert_equal 'off', @object.alarm_state
  assert !@object.saved
end
test_should_raise_exception_if_invalid_event_specified() click to toggle source
# File test/unit/machine_collection_test.rb, line 146
def test_should_raise_exception_if_invalid_event_specified
  exception = assert_raise(StateMachine::InvalidEvent) { @machines.fire_events(@object, :invalid) }
  assert_equal :invalid, exception.event
  
  exception = assert_raise(StateMachine::InvalidEvent) { @machines.fire_events(@object, :ignite, :invalid) }
  assert_equal :invalid, exception.event
end
test_should_run_failure_callbacks_if_any_event_cannot_transition() click to toggle source
# File test/unit/machine_collection_test.rb, line 166
def test_should_run_failure_callbacks_if_any_event_cannot_transition
  @machines[:state].after_failure {@state_failure_run = true}
  @machines[:alarm_state].after_failure {@alarm_state_failure_run = true}
  
  assert !@machines.fire_events(@object, :park, :disable_alarm)
  assert @state_failure_run
  assert !@alarm_state_failure_run
end