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::StateTransition do
|
|
|
|
it 'should set from, to, and opts attr readers' do
|
|
|
|
opts = {:from => 'foo', :to => 'bar', :guard => 'g'}
|
|
|
|
st = AASM::SupportingClasses::StateTransition.new(opts)
|
|
|
|
|
|
|
|
st.from.should == opts[:from]
|
|
|
|
st.to.should == opts[:to]
|
|
|
|
st.opts.should == opts
|
|
|
|
end
|
2008-02-21 10:16:08 -05:00
|
|
|
|
|
|
|
it 'should pass equality check if from and to are the same' do
|
|
|
|
opts = {:from => 'foo', :to => 'bar', :guard => 'g'}
|
|
|
|
st = AASM::SupportingClasses::StateTransition.new(opts)
|
|
|
|
|
|
|
|
obj = mock('object')
|
|
|
|
obj.stub!(:from).and_return(opts[:from])
|
|
|
|
obj.stub!(:to).and_return(opts[:to])
|
|
|
|
|
|
|
|
st.should == obj
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should fail equality check if from are not the same' do
|
|
|
|
opts = {:from => 'foo', :to => 'bar', :guard => 'g'}
|
|
|
|
st = AASM::SupportingClasses::StateTransition.new(opts)
|
|
|
|
|
|
|
|
obj = mock('object')
|
|
|
|
obj.stub!(:from).and_return('blah')
|
|
|
|
obj.stub!(:to).and_return(opts[:to])
|
|
|
|
|
|
|
|
st.should_not == obj
|
|
|
|
end
|
2009-04-09 01:25:16 -04:00
|
|
|
|
2008-02-21 10:16:08 -05:00
|
|
|
it 'should fail equality check if to are not the same' do
|
|
|
|
opts = {:from => 'foo', :to => 'bar', :guard => 'g'}
|
|
|
|
st = AASM::SupportingClasses::StateTransition.new(opts)
|
|
|
|
|
|
|
|
obj = mock('object')
|
|
|
|
obj.stub!(:from).and_return(opts[:from])
|
|
|
|
obj.stub!(:to).and_return('blah')
|
|
|
|
|
|
|
|
st.should_not == obj
|
|
|
|
end
|
2008-01-07 14:11:38 -05:00
|
|
|
end
|
|
|
|
|
2008-02-21 10:46:06 -05:00
|
|
|
describe AASM::SupportingClasses::StateTransition, '- when performing guard checks' do
|
|
|
|
it 'should return true of there is no guard' do
|
|
|
|
opts = {:from => 'foo', :to => 'bar'}
|
|
|
|
st = AASM::SupportingClasses::StateTransition.new(opts)
|
|
|
|
|
|
|
|
st.perform(nil).should be_true
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should call the method on the object if guard is a symbol' do
|
|
|
|
opts = {:from => 'foo', :to => 'bar', :guard => :test}
|
|
|
|
st = AASM::SupportingClasses::StateTransition.new(opts)
|
|
|
|
|
|
|
|
obj = mock('object')
|
|
|
|
obj.should_receive(:test)
|
2009-04-09 01:25:16 -04:00
|
|
|
|
2008-02-21 10:46:06 -05:00
|
|
|
st.perform(obj)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should call the method on the object if guard is a string' do
|
|
|
|
opts = {:from => 'foo', :to => 'bar', :guard => 'test'}
|
|
|
|
st = AASM::SupportingClasses::StateTransition.new(opts)
|
|
|
|
|
|
|
|
obj = mock('object')
|
|
|
|
obj.should_receive(:test)
|
2009-04-09 01:25:16 -04:00
|
|
|
|
2008-02-21 10:46:06 -05:00
|
|
|
st.perform(obj)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should call the proc passing the object if the guard is a proc' do
|
|
|
|
opts = {:from => 'foo', :to => 'bar', :guard => Proc.new {|o| o.test}}
|
|
|
|
st = AASM::SupportingClasses::StateTransition.new(opts)
|
|
|
|
|
|
|
|
obj = mock('object')
|
|
|
|
obj.should_receive(:test)
|
|
|
|
|
|
|
|
st.perform(obj)
|
|
|
|
end
|
|
|
|
end
|