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

101 lines
3.2 KiB
Ruby
Raw Normal View History

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
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