2012-10-26 03:01:16 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2012-12-02 02:51:48 -05:00
|
|
|
describe 'callbacks for the new DSL' do
|
|
|
|
let(:callback) {CallbackNewDsl.new}
|
|
|
|
|
|
|
|
it "be called in order" do
|
|
|
|
callback.should_receive(:exit_open).once.ordered
|
|
|
|
callback.should_receive(:before).once.ordered
|
|
|
|
callback.should_receive(:before_exit_open).once.ordered # these should be before the state changes
|
|
|
|
callback.should_receive(:before_enter_closed).once.ordered
|
|
|
|
callback.should_receive(:enter_closed).once.ordered
|
|
|
|
callback.should_receive(:aasm_write_state).once.ordered.and_return(true) # this is when the state changes
|
|
|
|
callback.should_receive(:after_exit_open).once.ordered # these should be after the state changes
|
|
|
|
callback.should_receive(:after_enter_closed).once.ordered
|
|
|
|
callback.should_receive(:after).once.ordered
|
|
|
|
|
|
|
|
callback.close!
|
2012-10-26 03:01:16 -04:00
|
|
|
end
|
|
|
|
end
|
2012-11-28 04:42:41 -05:00
|
|
|
|
2012-12-02 02:51:48 -05:00
|
|
|
describe 'event callbacks' do
|
2012-11-28 04:42:41 -05:00
|
|
|
describe "with an error callback defined" do
|
|
|
|
before do
|
|
|
|
class Foo
|
2013-11-30 15:36:28 -05:00
|
|
|
aasm do
|
|
|
|
event :safe_close, :success => :success_callback, :error => :error_callback do
|
|
|
|
transitions :to => :closed, :from => [:open]
|
|
|
|
end
|
2012-11-28 04:42:41 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@foo = Foo.new
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should run error_callback if an exception is raised and error_callback defined" do
|
2012-12-02 02:51:48 -05:00
|
|
|
def @foo.error_callback(e); end
|
|
|
|
|
2013-11-15 05:19:26 -05:00
|
|
|
@foo.stub(:enter).and_raise(e=StandardError.new)
|
2012-11-28 04:42:41 -05:00
|
|
|
@foo.should_receive(:error_callback).with(e)
|
2012-12-02 02:51:48 -05:00
|
|
|
|
2012-11-28 04:42:41 -05:00
|
|
|
@foo.safe_close!
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should raise NoMethodError if exceptionis raised and error_callback is declared but not defined" do
|
2013-11-15 05:19:26 -05:00
|
|
|
@foo.stub(:enter).and_raise(StandardError)
|
2012-11-28 04:42:41 -05:00
|
|
|
lambda{@foo.safe_close!}.should raise_error(NoMethodError)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should propagate an error if no error callback is declared" do
|
2013-11-15 05:19:26 -05:00
|
|
|
@foo.stub(:enter).and_raise("Cannot enter safe")
|
2012-11-28 04:42:41 -05:00
|
|
|
lambda{@foo.close!}.should raise_error(StandardError, "Cannot enter safe")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "with aasm_event_fired defined" do
|
|
|
|
before do
|
|
|
|
@foo = Foo.new
|
2012-12-02 02:51:48 -05:00
|
|
|
def @foo.aasm_event_fired(event, from, to); end
|
2012-11-28 04:42:41 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should call it for successful bang fire' do
|
|
|
|
@foo.should_receive(:aasm_event_fired).with(:close, :open, :closed)
|
|
|
|
@foo.close!
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should call it for successful non-bang fire' do
|
|
|
|
@foo.should_receive(:aasm_event_fired)
|
|
|
|
@foo.close
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should not call it for failing bang fire' do
|
2013-11-15 05:19:26 -05:00
|
|
|
@foo.aasm.stub(:set_current_state_with_persistence).and_return(false)
|
2012-11-28 04:42:41 -05:00
|
|
|
@foo.should_not_receive(:aasm_event_fired)
|
|
|
|
@foo.close!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "with aasm_event_failed defined" do
|
|
|
|
before do
|
|
|
|
@foo = Foo.new
|
2012-12-02 02:51:48 -05:00
|
|
|
def @foo.aasm_event_failed(event, from); end
|
2012-11-28 04:42:41 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should call it when transition failed for bang fire' do
|
|
|
|
@foo.should_receive(:aasm_event_failed).with(:null, :open)
|
|
|
|
lambda {@foo.null!}.should raise_error(AASM::InvalidTransition)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should call it when transition failed for non-bang fire' do
|
|
|
|
@foo.should_receive(:aasm_event_failed).with(:null, :open)
|
|
|
|
lambda {@foo.null}.should raise_error(AASM::InvalidTransition)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should not call it if persist fails for bang fire' do
|
2013-11-15 05:19:26 -05:00
|
|
|
@foo.aasm.stub(:set_current_state_with_persistence).and_return(false)
|
2012-11-28 04:42:41 -05:00
|
|
|
@foo.should_receive(:aasm_event_failed)
|
|
|
|
@foo.close!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|