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

88 lines
2.3 KiB
Ruby
Raw Normal View History

2010-08-27 16:35:26 -04:00
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'spec_helper'))
2008-01-07 14:11:38 -05:00
describe AASM::SupportingClasses::State do
before(:each) do
@name = :astate
@options = { :crazy_custom_key => 'key' }
2008-01-07 14:11:38 -05:00
end
def new_state(options={})
AASM::SupportingClasses::State.new(@name, @options.merge(options))
2008-01-07 14:11:38 -05:00
end
it 'should set the name' do
state = new_state
state.name.should == :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
new_state.display_name.should == 'Astate'
end
2009-04-09 01:25:16 -04:00
it 'should set the display_name from options' do
new_state(:display => "A State").display_name.should == 'A State'
end
2008-01-07 14:11:38 -05:00
it 'should set the options and expose them as options' do
new_state.options.should == @options
end
2008-01-07 14:11:38 -05:00
it 'should be equal to a symbol of the same name' do
new_state.should == :astate
2008-01-07 14:11:38 -05:00
end
it 'should be equal to a State of the same name' do
new_state.should == 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
record = mock('record')
record.should_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
record = mock('record')
record.should_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 }])
record = mock('record')
record.should_receive(:a)
record.should_receive(:b)
record.should_receive(:c)
record.should_receive(:foobar)
2011-11-26 14:07:56 -05:00
state.fire_callbacks(:entering, 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])
record = mock('record')
record.should_receive(:a)
record.should_receive(:b).and_throw(:halt_aasm_chain)
record.should_not_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
record = mock('record')
record.should_receive(:foobar)
2009-04-09 01:25:16 -04:00
2011-11-26 14:07:56 -05:00
state.fire_callbacks(:entering, record)
2008-01-07 14:11:38 -05:00
end
end