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

296 lines
11 KiB
Ruby
Raw Permalink Normal View History

2012-10-26 03:01:16 -04:00
require 'spec_helper'
2014-11-25 16:49:21 -05:00
Dir[File.dirname(__FILE__) + "/../models/callbacks/*.rb"].sort.each { |f| require File.expand_path(f) }
2012-10-26 03:01:16 -04:00
2012-12-02 02:51:48 -05:00
describe 'callbacks for the new DSL' do
it "be called in order" do
2014-12-05 11:20:32 -05:00
show_debug_log = false
callback = Callbacks::Basic.new(:log => show_debug_log)
2014-09-09 17:02:11 -04:00
callback.aasm.current_state
2014-12-05 11:20:32 -05:00
unless show_debug_log
expect(callback).to receive(:before_event).once.ordered
expect(callback).to receive(:event_guard).once.ordered.and_return(true)
expect(callback).to receive(:transition_guard).once.ordered.and_return(true)
expect(callback).to receive(:before_exit_open).once.ordered # these should be before the state changes
expect(callback).to receive(:exit_open).once.ordered
# expect(callback).to receive(:event_guard).once.ordered.and_return(true)
# expect(callback).to receive(:transition_guard).once.ordered.and_return(true)
expect(callback).to receive(:after_transition).once.ordered
expect(callback).to receive(:before_enter_closed).once.ordered
expect(callback).to receive(:enter_closed).once.ordered
expect(callback).to receive(:aasm_write_state).once.ordered.and_return(true) # this is when the state changes
expect(callback).to receive(:after_exit_open).once.ordered # these should be after the state changes
expect(callback).to receive(:after_enter_closed).once.ordered
expect(callback).to receive(:after_event).once.ordered
end
2012-12-02 02:51:48 -05:00
2014-09-09 17:02:11 -04:00
# puts "------- close!"
2012-12-02 02:51:48 -05:00
callback.close!
2012-10-26 03:01:16 -04:00
end
2014-09-09 17:02:11 -04:00
it "does not run any state callback if the event guard fails" do
2014-11-25 16:49:21 -05:00
callback = Callbacks::Basic.new(:log => false)
2014-09-09 17:02:11 -04:00
callback.aasm.current_state
2014-12-05 11:20:32 -05:00
expect(callback).to receive(:before_event).once.ordered
2014-09-09 17:02:11 -04:00
expect(callback).to receive(:event_guard).once.ordered.and_return(false)
expect(callback).to_not receive(:transition_guard)
expect(callback).to_not receive(:before_exit_open)
expect(callback).to_not receive(:exit_open)
2014-12-05 11:20:32 -05:00
expect(callback).to_not receive(:after_transition)
2014-09-09 17:02:11 -04:00
expect(callback).to_not receive(:before_enter_closed)
expect(callback).to_not receive(:enter_closed)
expect(callback).to_not receive(:aasm_write_state)
expect(callback).to_not receive(:after_exit_open)
expect(callback).to_not receive(:after_enter_closed)
2014-12-05 11:20:32 -05:00
expect(callback).to_not receive(:after_event)
2014-09-09 17:02:11 -04:00
expect {
callback.close!
}.to raise_error(AASM::InvalidTransition)
end
2015-05-26 05:19:54 -04:00
it "handles private callback methods as well" do
show_debug_log = false
callback = Callbacks::PrivateMethod.new(:log => show_debug_log)
callback.aasm.current_state
# puts "------- close!"
expect {
callback.close!
}.to_not raise_error
end
context "if the transition guard fails" do
it "does not run any state callback if guard is defined inline" do
show_debug_log = false
2014-11-25 16:49:21 -05:00
callback = Callbacks::Basic.new(:log => show_debug_log, :fail_transition_guard => true)
callback.aasm.current_state
2014-09-09 17:02:11 -04:00
unless show_debug_log
2014-12-05 11:20:32 -05:00
expect(callback).to receive(:before_event).once.ordered
expect(callback).to receive(:event_guard).once.ordered.and_return(true)
expect(callback).to receive(:transition_guard).once.ordered.and_return(false)
expect(callback).to_not receive(:before_exit_open)
expect(callback).to_not receive(:exit_open)
2014-12-05 11:20:32 -05:00
expect(callback).to_not receive(:after_transition)
expect(callback).to_not receive(:before_enter_closed)
expect(callback).to_not receive(:enter_closed)
expect(callback).to_not receive(:aasm_write_state)
expect(callback).to_not receive(:after_exit_open)
expect(callback).to_not receive(:after_enter_closed)
2014-12-05 11:20:32 -05:00
expect(callback).to_not receive(:after_event)
end
2014-09-09 17:02:11 -04:00
expect {
callback.close!
}.to raise_error(AASM::InvalidTransition)
end
it "does not run transition_guard twice for multiple permitted transitions" do
show_debug_log = false
callback = Callbacks::MultipleTransitionsTransitionGuard.new(:log => show_debug_log, :fail_transition_guard => true)
callback.aasm.current_state
unless show_debug_log
expect(callback).to receive(:before).once.ordered
expect(callback).to receive(:event_guard).once.ordered.and_return(true)
expect(callback).to receive(:transition_guard).once.ordered.and_return(false)
expect(callback).to receive(:event_guard).once.ordered.and_return(true)
expect(callback).to receive(:before_exit_open).once.ordered
expect(callback).to receive(:exit_open).once.ordered
expect(callback).to receive(:aasm_write_state).once.ordered.and_return(true) # this is when the state changes
expect(callback).to receive(:after_exit_open).once.ordered
expect(callback).to receive(:after).once.ordered
expect(callback).to_not receive(:transitioning)
expect(callback).to_not receive(:before_enter_closed)
expect(callback).to_not receive(:enter_closed)
expect(callback).to_not receive(:after_enter_closed)
end
callback.close!
expect(callback.aasm.current_state).to eql :failed
end
it "does not run any state callback if guard is defined with block" do
2014-11-25 16:49:21 -05:00
callback = Callbacks::GuardWithinBlock.new #(:log => true, :fail_transition_guard => true)
callback.aasm.current_state
expect(callback).to receive(:before).once.ordered
expect(callback).to receive(:event_guard).once.ordered.and_return(true)
expect(callback).to receive(:transition_guard).once.ordered.and_return(false)
expect(callback).to_not receive(:before_exit_open)
expect(callback).to_not receive(:exit_open)
expect(callback).to_not receive(:transitioning)
expect(callback).to_not receive(:before_enter_closed)
expect(callback).to_not receive(:enter_closed)
expect(callback).to_not receive(:aasm_write_state)
expect(callback).to_not receive(:after_exit_open)
expect(callback).to_not receive(:after_enter_closed)
expect(callback).to_not receive(:after)
expect {
callback.close!
}.to raise_error(AASM::InvalidTransition)
end
2014-09-09 17:02:11 -04:00
end
it "should properly pass arguments" do
cb = Callbacks::WithArgs.new(:log => false)
cb.aasm.current_state
cb.reset_data
cb.close!(:arg1, :arg2)
2015-03-04 05:55:34 -05:00
expect(cb.data).to eql 'before(:arg1,:arg2) before_exit_open(:arg1,:arg2) transition_proc(:arg1,:arg2) before_enter_closed(:arg1,:arg2) aasm_write_state after_exit_open(:arg1,:arg2) after_enter_closed(:arg1,:arg2) after(:arg1,:arg2)'
end
it "should call the callbacks given the to-state as argument" do
2014-11-25 16:49:21 -05:00
cb = Callbacks::WithStateArg.new
2014-12-14 09:05:16 -05:00
expect(cb).to receive(:before_method).with(:arg1).once.ordered
expect(cb).to receive(:transition_method).never
expect(cb).to receive(:transition_method2).with(:arg1).once.ordered
expect(cb).to receive(:after_method).with(:arg1).once.ordered
cb.close!(:out_to_lunch, :arg1)
2014-11-25 16:49:21 -05:00
cb = Callbacks::WithStateArg.new
some_object = double('some object')
2014-12-14 09:05:16 -05:00
expect(cb).to receive(:before_method).with(some_object).once.ordered
expect(cb).to receive(:transition_method2).with(some_object).once.ordered
expect(cb).to receive(:after_method).with(some_object).once.ordered
cb.close!(:out_to_lunch, some_object)
end
it "should call the proper methods just with arguments" do
2014-11-25 16:49:21 -05:00
cb = Callbacks::WithStateArg.new
2014-12-14 09:05:16 -05:00
expect(cb).to receive(:before_method).with(:arg1).once.ordered
expect(cb).to receive(:transition_method).with(:arg1).once.ordered
expect(cb).to receive(:transition_method).never
expect(cb).to receive(:after_method).with(:arg1).once.ordered
cb.close!(:arg1)
2014-11-25 16:49:21 -05:00
cb = Callbacks::WithStateArg.new
some_object = double('some object')
2014-12-14 09:05:16 -05:00
expect(cb).to receive(:before_method).with(some_object).once.ordered
expect(cb).to receive(:transition_method).with(some_object).once.ordered
expect(cb).to receive(:transition_method).never
expect(cb).to receive(:after_method).with(some_object).once.ordered
cb.close!(some_object)
end
2012-10-26 03:01:16 -04:00
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
# this hack is needed to allow testing of parameters, since RSpec
# destroys a method's arity when mocked
attr_accessor :data
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
context "error_callback defined" do
it "should run error_callback if an exception is raised" do
def @foo.error_callback(e)
@data = [e]
end
allow(@foo).to receive(:before_enter).and_raise(e = StandardError.new)
@foo.safe_close!
expect(@foo.data).to eql [e]
end
it "should run error_callback without parameters if callback does not support any" do
def @foo.error_callback(e)
@data = []
end
2012-12-02 02:51:48 -05:00
allow(@foo).to receive(:before_enter).and_raise(e = StandardError.new)
2012-12-02 02:51:48 -05:00
@foo.safe_close!('arg1', 'arg2')
expect(@foo.data).to eql []
end
it "should run error_callback with parameters if callback supports them" do
def @foo.error_callback(e, arg1, arg2)
@data = [arg1, arg2]
end
allow(@foo).to receive(:before_enter).and_raise(e = StandardError.new)
@foo.safe_close!('arg1', 'arg2')
expect(@foo.data).to eql ['arg1', 'arg2']
end
2012-11-28 04:42:41 -05:00
end
it "should raise NoMethodError if exception is raised and error_callback is declared but not defined" do
2014-09-09 17:02:11 -04:00
allow(@foo).to receive(:before_enter).and_raise(StandardError)
expect{@foo.safe_close!}.to raise_error(NoMethodError)
2012-11-28 04:42:41 -05:00
end
it "should propagate an error if no error callback is declared" do
2014-09-09 17:02:11 -04:00
allow(@foo).to receive(:before_enter).and_raise("Cannot enter safe")
expect{@foo.close!}.to raise_error(StandardError, "Cannot enter safe")
2012-11-28 04:42:41 -05:00
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
expect(@foo).to receive(:aasm_event_fired).with(:close, :open, :closed)
2012-11-28 04:42:41 -05:00
@foo.close!
end
it 'should call it for successful non-bang fire' do
expect(@foo).to receive(:aasm_event_fired)
2012-11-28 04:42:41 -05:00
@foo.close
end
it 'should not call it for failing bang fire' do
allow(@foo.aasm).to receive(:set_current_state_with_persistence).and_return(false)
expect(@foo).not_to receive(:aasm_event_fired)
2012-11-28 04:42:41 -05:00
@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
expect(@foo).to receive(:aasm_event_failed).with(:null, :open)
expect {@foo.null!}.to raise_error(AASM::InvalidTransition)
2012-11-28 04:42:41 -05:00
end
it 'should call it when transition failed for non-bang fire' do
expect(@foo).to receive(:aasm_event_failed).with(:null, :open)
expect {@foo.null}.to raise_error(AASM::InvalidTransition)
2012-11-28 04:42:41 -05:00
end
it 'should not call it if persist fails for bang fire' do
allow(@foo.aasm).to receive(:set_current_state_with_persistence).and_return(false)
expect(@foo).to receive(:aasm_event_failed)
2012-11-28 04:42:41 -05:00
@foo.close!
end
end
2012-11-28 04:42:41 -05:00
end