1
0
Fork 0
mirror of https://github.com/aasm/aasm synced 2023-03-27 23:22:41 -04:00
aasm/spec/unit/state_spec.rb

90 lines
2.4 KiB
Ruby
Raw Normal View History

2012-11-28 04:58:52 -05:00
require 'spec_helper'
2008-01-07 14:11:38 -05:00
describe AASM::Core::State do
let(:state_machine) { AASM::StateMachine.new(:name) }
2008-01-07 14:11:38 -05:00
before(:each) do
@name = :astate
@options = { :crazy_custom_key => 'key' }
2008-01-07 14:11:38 -05:00
end
def new_state(options={})
AASM::Core::State.new(@name, Conversation, state_machine, @options.merge(options))
2008-01-07 14:11:38 -05:00
end
it 'should set the name' do
state = new_state
expect(state.name).to eq(:astate)
2008-01-07 14:11:38 -05:00
end
2009-04-09 01:25:16 -04:00
it 'should set the display_name from name' do
expect(new_state.display_name).to eq('Astate')
end
2009-04-09 01:25:16 -04:00
it 'should set the display_name from options' do
expect(new_state(:display => "A State").display_name).to eq('A State')
end
2008-01-07 14:11:38 -05:00
it 'should set the options and expose them as options' do
expect(new_state.options).to eq(@options)
end
2008-01-07 14:11:38 -05:00
it 'should be equal to a symbol of the same name' do
expect(new_state).to eq(:astate)
2008-01-07 14:11:38 -05:00
end
it 'should be equal to a State of the same name' do
expect(new_state).to eq(new_state)
2008-01-07 14:11:38 -05:00
end
it 'should send a message to the record for an action if the action is present as a symbol' do
state = new_state(:entering => :foo)
2008-01-07 14:11:38 -05:00
2013-11-15 05:19:26 -05:00
record = double('record')
expect(record).to receive(:foo)
2008-01-07 14:11:38 -05:00
2011-11-26 14:07:56 -05:00
state.fire_callbacks(:entering, record)
2008-01-07 14:11:38 -05:00
end
it 'should send a message to the record for an action if the action is present as a string' do
state = new_state(:entering => 'foo')
2008-01-07 14:11:38 -05:00
2013-11-15 05:19:26 -05:00
record = double('record')
expect(record).to receive(:foo)
2011-11-26 14:07:56 -05:00
state.fire_callbacks(:entering, record)
2008-01-07 14:11:38 -05:00
end
it 'should send a message to the record for each action' do
state = new_state(:entering => [:a, :b, "c", lambda {|r| r.foobar }])
2013-11-15 05:19:26 -05:00
record = double('record')
expect(record).to receive(:a)
expect(record).to receive(:b)
expect(record).to receive(:c)
expect(record).to receive(:foobar)
state.fire_callbacks(:entering, record, record)
end
2008-01-07 14:11:38 -05:00
it "should stop calling actions if one of them raises :halt_aasm_chain" do
state = new_state(:entering => [:a, :b, :c])
2013-11-15 05:19:26 -05:00
record = double('record')
expect(record).to receive(:a)
expect(record).to receive(:b).and_throw(:halt_aasm_chain)
expect(record).not_to receive(:c)
2011-11-26 14:07:56 -05:00
state.fire_callbacks(:entering, record)
end
it 'should call a proc, passing in the record for an action if the action is present' do
state = new_state(:entering => Proc.new {|r| r.foobar})
2008-01-07 14:11:38 -05:00
2013-11-15 05:19:26 -05:00
record = double('record')
expect(record).to receive(:foobar)
2009-04-09 01:25:16 -04:00
state.fire_callbacks(:entering, record, record)
2008-01-07 14:11:38 -05:00
end
end