Class: MachineCollectionFireExplicitTest

Inherits:
Test::Unit::TestCase
  • Object
show all
Defined in:
lib/branston/vendor/plugins/state_machine/test/unit/machine_collection_test.rb

Instance Method Summary collapse

Instance Method Details

#setupObject



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/branston/vendor/plugins/state_machine/test/unit/machine_collection_test.rb', line 107

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_transitionObject



159
160
161
162
163
164
# File 'lib/branston/vendor/plugins/state_machine/test/unit/machine_collection_test.rb', line 159

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_transitionObject



147
148
149
150
151
152
153
154
155
156
157
# File 'lib/branston/vendor/plugins/state_machine/test/unit/machine_collection_test.rb', line 147

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_actionObject



166
167
168
169
170
171
# File 'lib/branston/vendor/plugins/state_machine/test/unit/machine_collection_test.rb', line 166

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_specifiedObject



139
140
141
142
143
144
145
# File 'lib/branston/vendor/plugins/state_machine/test/unit/machine_collection_test.rb', line 139

def test_should_raise_exception_if_invalid_event_specified
  exception = assert_raise(StateMachine::InvalidEvent) { @machines.fire_events(@object, :invalid) }
  assert_equal ':invalid is an unknown state machine event', exception.message
  
  exception = assert_raise(StateMachine::InvalidEvent) { @machines.fire_events(@object, :ignite, :invalid) }
  assert_equal ':invalid is an unknown state machine event', exception.message
end