-
Notifications
You must be signed in to change notification settings - Fork 629
/
callbacks_spec.rb
148 lines (123 loc) · 5.54 KB
/
callbacks_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
require 'spec_helper'
describe 'callbacks for the old DSL' do
let(:callback) {CallbackOldDsl.new}
it "should get close callbacks" 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!
end
end
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!
end
it "should properly pass arguments" do
cb = CallbackNewDslArgs.new
cb.should_receive(:exit_open).once.ordered
cb.should_receive(:before).with(:arg1, :arg2).once.ordered
cb.should_receive(:transition_proc).with(:arg1, :arg2).once.ordered
cb.should_receive(:before_exit_open).once.ordered # these should be before the state changes
cb.should_receive(:before_enter_closed).once.ordered
cb.should_receive(:enter_closed).once.ordered
cb.should_receive(:aasm_write_state).once.ordered.and_return(true) # this is when the state changes
cb.should_receive(:after_exit_open).once.ordered # these should be after the state changes
cb.should_receive(:after_enter_closed).once.ordered
cb.should_receive(:after).with(:arg1, :arg2).once.ordered
cb.close!(:arg1, :arg2)
end
it "should call the proper methods given a to state as the first arg" do
cb = CallbackWithStateArg.new
cb.should_receive(:before_method).with(:arg1).once.ordered
cb.should_receive(:transition_method).with(:arg1).once.ordered
cb.should_receive(:transition_method).never
cb.should_receive(:after_method).with(:arg1).once.ordered
cb.close!(:arg1)
cb = CallbackWithStateArg.new
cb.should_receive(:before_method).with(:arg1).once.ordered
cb.should_receive(:transition_method).never
cb.should_receive(:transition_method2).with(:arg1).once.ordered
cb.should_receive(:after_method).with(:arg1).once.ordered
cb.close!(:out_to_lunch, :arg1)
end
end
describe 'event callbacks' do
describe "with an error callback defined" do
before do
class Foo
aasm_event :safe_close, :success => :success_callback, :error => :error_callback do
transitions :to => :closed, :from => [:open]
end
end
@foo = Foo.new
end
it "should run error_callback if an exception is raised and error_callback defined" do
def @foo.error_callback(e); end
@foo.stub!(:enter).and_raise(e=StandardError.new)
@foo.should_receive(:error_callback).with(e)
@foo.safe_close!
end
it "should raise NoMethodError if exceptionis raised and error_callback is declared but not defined" do
@foo.stub!(:enter).and_raise(StandardError)
lambda{@foo.safe_close!}.should raise_error(NoMethodError)
end
it "should propagate an error if no error callback is declared" do
@foo.stub!(:enter).and_raise("Cannot enter safe")
lambda{@foo.close!}.should raise_error(StandardError, "Cannot enter safe")
end
end
describe "with aasm_event_fired defined" do
before do
@foo = Foo.new
def @foo.aasm_event_fired(event, from, to); end
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
@foo.aasm.stub!(:set_current_state_with_persistence).and_return(false)
@foo.should_not_receive(:aasm_event_fired)
@foo.close!
end
end
describe "with aasm_event_failed defined" do
before do
@foo = Foo.new
def @foo.aasm_event_failed(event, from); end
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
@foo.aasm.stub!(:set_current_state_with_persistence).and_return(false)
@foo.should_receive(:aasm_event_failed)
@foo.close!
end
end
end