2009-03-20 06:32:24 -04:00
|
|
|
require 'cases/helper'
|
2008-06-28 12:19:44 -04:00
|
|
|
|
|
|
|
class EventTest < ActiveModel::TestCase
|
|
|
|
def setup
|
2008-11-22 21:21:48 -05:00
|
|
|
@state_name = :close_order
|
2008-06-28 12:19:44 -04:00
|
|
|
@success = :success_callback
|
|
|
|
end
|
|
|
|
|
|
|
|
def new_event
|
2008-11-22 21:21:48 -05:00
|
|
|
@event = ActiveModel::StateMachine::Event.new(nil, @state_name, {:success => @success}) do
|
2008-06-28 12:19:44 -04:00
|
|
|
transitions :to => :closed, :from => [:open, :received]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'should set the name' do
|
2008-11-22 21:21:48 -05:00
|
|
|
assert_equal @state_name, new_event.name
|
2008-06-28 12:19:44 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test 'should set the success option' do
|
2008-06-28 14:33:50 -04:00
|
|
|
assert_equal @success, new_event.success
|
2008-06-28 12:19:44 -04:00
|
|
|
end
|
|
|
|
|
2008-11-19 14:08:34 -05:00
|
|
|
test 'should create StateTransitions' do
|
|
|
|
ActiveModel::StateMachine::StateTransition.expects(:new).with(:to => :closed, :from => :open)
|
|
|
|
ActiveModel::StateMachine::StateTransition.expects(:new).with(:to => :closed, :from => :received)
|
|
|
|
new_event
|
2008-06-28 12:19:44 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class EventBeingFiredTest < ActiveModel::TestCase
|
|
|
|
test 'should raise an AASM::InvalidTransition error if the transitions are empty' do
|
2008-06-28 14:01:40 -04:00
|
|
|
event = ActiveModel::StateMachine::Event.new(nil, :event)
|
2008-06-28 12:19:44 -04:00
|
|
|
|
2009-03-08 16:11:58 -04:00
|
|
|
assert_raise ActiveModel::StateMachine::InvalidTransition do
|
2008-06-28 12:19:44 -04:00
|
|
|
event.fire(nil)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'should return the state of the first matching transition it finds' do
|
2008-06-28 14:01:40 -04:00
|
|
|
event = ActiveModel::StateMachine::Event.new(nil, :event) do
|
2008-06-28 12:19:44 -04:00
|
|
|
transitions :to => :closed, :from => [:open, :received]
|
|
|
|
end
|
|
|
|
|
|
|
|
obj = stub
|
|
|
|
obj.stubs(:current_state).returns(:open)
|
|
|
|
|
|
|
|
assert_equal :closed, event.fire(obj)
|
|
|
|
end
|
|
|
|
end
|