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

Add specs for new behavior, experiment with error matching

This commit is contained in:
Chris Woodrich 2015-10-22 01:21:09 -07:00
parent b6d5a0d7fa
commit 9702bf7a88
5 changed files with 86 additions and 13 deletions

View file

@ -73,7 +73,7 @@ module AASM::Core
end
def failed_callbacks
@transitions.flat_map(&:failures)
transitions.flat_map(&:failures)
end
private

View file

@ -57,7 +57,9 @@ module AASM::Core
failures << code unless result
result
when Proc
code.arity == 0 ? record.instance_exec(&code) : record.instance_exec(*args, &code)
result = (code.arity == 0 ? record.instance_exec(&code) : record.instance_exec(*args, &code))
failures << code.source_location.join('#') unless result
result
when Array
if options[:guard]
# invoke guard callbacks

View file

@ -1,6 +1,16 @@
require 'spec_helper'
Dir[File.dirname(__FILE__) + "/../models/callbacks/*.rb"].sort.each { |f| require File.expand_path(f) }
def safe_error(callback = nil)
error = nil
begin
yield
rescue Exception => e
error = e
return error
end
end
describe 'callbacks for the new DSL' do
it "be called in order" do
@ -47,9 +57,13 @@ describe 'callbacks for the new DSL' do
expect(callback).to_not receive(:after_enter_closed)
expect(callback).to_not receive(:after_event)
expect {
callback.left_close!
}.to raise_error(AASM::InvalidTransition)
error = safe_error { callback.left_close! }
expect(error.class).to eq AASM::InvalidTransition
expect(error.message).to eq(
"Event 'left_close' cannot transition from 'open'. Failed callback(s): [:after_transition, :event_guard]."
)
end
it "handles private callback methods as well" do
@ -85,9 +99,12 @@ describe 'callbacks for the new DSL' do
expect(callback).to_not receive(:after_event)
end
expect {
callback.left_close!
}.to raise_error(AASM::InvalidTransition)
error = safe_error { callback.left_close! }
expect(error.class).to eq AASM::InvalidTransition
expect(error.message).to eq(
"Event 'left_close' cannot transition from 'open'. Failed callback(s): [:after_transition, :event_guard, :transition_guard]."
)
end
it "does not run transition_guard twice for multiple permitted transitions" do
@ -133,9 +150,10 @@ describe 'callbacks for the new DSL' do
expect(callback).to_not receive(:after_enter_closed)
expect(callback).to_not receive(:after)
expect {
callback.close!
}.to raise_error(AASM::InvalidTransition)
error = safe_error(callback) { callback.close! }
expect(error.class).to eq AASM::InvalidTransition
expect(error.message).to eq "Event 'close' cannot transition from 'open'. Failed callback(s): [\"/Users/woodrich/Dropbox/personal/aasm/spec/models/callbacks/guard_within_block_multiple.rb#30\"]."
end
end
@ -277,12 +295,25 @@ describe 'event callbacks' do
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)
#expect {@foo.null!}.to raise_error(AASM::InvalidTransition)
error = safe_error { @foo.null! }
expect(error.class).to eq AASM::InvalidTransition
expect(error.message).to eq(
"Event 'null' cannot transition from 'open'. Failed callback(s): [:always_false]."
)
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)
error = safe_error { @foo.null }
expect(error.class).to eq AASM::InvalidTransition
expect(error.message).to eq(
"Event 'null' cannot transition from 'open'. Failed callback(s): [:always_false]."
)
end
it 'should not call it if persist fails for bang fire' do

View file

@ -109,6 +109,35 @@ describe 'firing an event' do
expect(event.fire(obj, {}, nil, 'arg1', 'arg2')).to eq(:closed)
end
context 'when given a gaurd proc' do
it 'should have access to callback failures in the transitions' do
event = AASM::Core::Event.new(:graduate, state_machine) do
transitions :to => :alumni, :from => [:student, :applicant],
:guard => Proc.new { 1 + 1 == 3 }
end
line_number = __LINE__ - 2
obj = double('object', :aasm => double('aasm', :current_state => :student))
event.fire(obj, {}, nil)
expect(event.failed_callbacks).to eq ["#{__FILE__}##{line_number}"]
end
end
context 'when given a guard symbol' do
it 'should have access to callback failures in the transitions' do
event = AASM::Core::Event.new(:graduate, state_machine) do
transitions :to => :alumni, :from => [:student, :applicant],
guard: :paid_tuition?
end
obj = double('object', :aasm => double('aasm', :current_state => :student))
allow(obj).to receive(:paid_tuition?).and_return(false)
event.fire(obj, {}, nil)
expect(event.failed_callbacks).to eq [:paid_tuition?]
end
end
end
describe 'should fire callbacks' do

View file

@ -145,6 +145,17 @@ describe AASM::Core::Transition, '- when performing guard checks' do
expect(st.allowed?(obj)).to be false
end
it 'should add the name of the failed method calls to the failures instance var' do
opts = {:from => 'foo', :to => 'bar', :guard => :test}
st = AASM::Core::Transition.new(event, opts)
obj = double('object')
expect(obj).to receive(:test)
st.allowed?(obj)
expect(st.failures).to eq [:test]
end
it 'should call the method on the object if unless is a symbol' do
opts = {:from => 'foo', :to => 'bar', :unless => :test}
st = AASM::Core::Transition.new(event, opts)