Class: MachineCollectionFireExplicitWithValidationsTest

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



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/branston/vendor/plugins/state_machine/test/unit/machine_collection_test.rb', line 235

def setup
  StateMachine::Integrations.const_set('Custom', Module.new do
    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

#teardownObject



282
283
284
# File 'lib/branston/vendor/plugins/state_machine/test/unit/machine_collection_test.rb', line 282

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

#test_should_invalidate_if_no_transitions_existObject



274
275
276
277
278
279
280
# File 'lib/branston/vendor/plugins/state_machine/test/unit/machine_collection_test.rb', line 274

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_alarm"'], @object.errors
end

#test_should_not_invalidate_if_transitions_existObject



269
270
271
272
# File 'lib/branston/vendor/plugins/state_machine/test/unit/machine_collection_test.rb', line 269

def test_should_not_invalidate_if_transitions_exist
  assert @machines.fire_events(@object, :ignite, :disable_alarm)
  assert_equal [], @object.errors
end