Class: MachineCollectionFireExplicitWithTransactionsTest

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



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/branston/vendor/plugins/state_machine/test/unit/machine_collection_test.rb', line 175

def setup
  @machines = StateMachine::MachineCollection.new
  
  @klass = Class.new do
    attr_accessor :allow_save
    
    def save
      @allow_save
    end
  end
  
  StateMachine::Integrations.const_set('Custom', Module.new do
    attr_reader :rolled_back
    
    def transaction(object)
      @rolled_back = yield
    end
  end)
  
  # First machine
  @machines[:state] = @state = StateMachine::Machine.new(@klass, :state, :initial => :parked, :action => :save, :integration => :custom)
  @state.event :ignite do
    transition :parked => :idling
  end
  
  # Second machine
  @machines[:alarm_state] = @alarm_state = StateMachine::Machine.new(@klass, :alarm_state, :initial => :active, :action => :save, :namespace => 'alarm', :integration => :custom)
  @alarm_state.event :disable do
    transition :active => :off
  end
  
  @object = @klass.new
end

#teardownObject



229
230
231
# File 'lib/branston/vendor/plugins/state_machine/test/unit/machine_collection_test.rb', line 229

def teardown
  StateMachine::Integrations.send(:remove_const, 'Custom')
end

#test_should_not_rollback_if_successfulObject



209
210
211
212
213
214
215
216
217
# File 'lib/branston/vendor/plugins/state_machine/test/unit/machine_collection_test.rb', line 209

def test_should_not_rollback_if_successful
  @object.allow_save = true
  
  assert @machines.fire_events(@object, :ignite, :disable_alarm)
  assert_equal true, @state.rolled_back
  assert_nil @alarm_state.rolled_back
  assert_equal 'idling', @object.state
  assert_equal 'off', @object.alarm_state
end

#test_should_rollback_if_not_successfulObject



219
220
221
222
223
224
225
226
227
# File 'lib/branston/vendor/plugins/state_machine/test/unit/machine_collection_test.rb', line 219

def test_should_rollback_if_not_successful
  @object.allow_save = false
  
  assert !@machines.fire_events(@object, :ignite, :disable_alarm)
  assert_equal false, @state.rolled_back
  assert_nil @alarm_state.rolled_back
  assert_equal 'parked', @object.state
  assert_equal 'active', @object.alarm_state
end