-
Notifications
You must be signed in to change notification settings - Fork 7
/
state_transition_spec.rb
179 lines (132 loc) · 5.17 KB
/
state_transition_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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'spec_helper'))
describe 'transitions' do
it 'should raise an exception when whiny' do
process = ProcessWithNewDsl.new
lambda { process.stop! }.should raise_error(AASM::InvalidTransition)
process.should be_sleeping
end
it 'should not raise an exception when whiny' do
silencer = Silencer.new
silencer.smile!.should be_false
silencer.should be_silent
end
end
describe AASM::SupportingClasses::StateTransition do
it 'should set from, to, and opts attr readers' do
opts = {:from => 'foo', :to => 'bar', :guard => 'g'}
st = AASM::SupportingClasses::StateTransition.new(opts)
st.from.should == opts[:from]
st.to.should == opts[:to]
st.opts.should == opts
end
it 'should pass equality check if from and to are the same' do
opts = {:from => 'foo', :to => 'bar', :guard => 'g'}
st = AASM::SupportingClasses::StateTransition.new(opts)
obj = mock('object')
obj.stub!(:from).and_return(opts[:from])
obj.stub!(:to).and_return(opts[:to])
st.should == obj
end
it 'should fail equality check if from are not the same' do
opts = {:from => 'foo', :to => 'bar', :guard => 'g'}
st = AASM::SupportingClasses::StateTransition.new(opts)
obj = mock('object')
obj.stub!(:from).and_return('blah')
obj.stub!(:to).and_return(opts[:to])
st.should_not == obj
end
it 'should fail equality check if to are not the same' do
opts = {:from => 'foo', :to => 'bar', :guard => 'g'}
st = AASM::SupportingClasses::StateTransition.new(opts)
obj = mock('object')
obj.stub!(:from).and_return(opts[:from])
obj.stub!(:to).and_return('blah')
st.should_not == obj
end
end
describe AASM::SupportingClasses::StateTransition, '- when performing guard checks' do
it 'should return true of there is no guard' do
opts = {:from => 'foo', :to => 'bar'}
st = AASM::SupportingClasses::StateTransition.new(opts)
st.perform(nil).should be_true
end
it 'should call the method on the object if guard is a symbol' do
opts = {:from => 'foo', :to => 'bar', :guard => :test}
st = AASM::SupportingClasses::StateTransition.new(opts)
obj = mock('object')
obj.should_receive(:test)
st.perform(obj)
end
it 'should call the method on the object if guard is a string' do
opts = {:from => 'foo', :to => 'bar', :guard => 'test'}
st = AASM::SupportingClasses::StateTransition.new(opts)
obj = mock('object')
obj.should_receive(:test)
st.perform(obj)
end
it 'should call the proc passing the object if the guard is a proc' do
opts = {:from => 'foo', :to => 'bar', :guard => Proc.new {|o| o.test}}
st = AASM::SupportingClasses::StateTransition.new(opts)
obj = mock('object')
obj.should_receive(:test)
st.perform(obj)
end
end
describe AASM::SupportingClasses::StateTransition, '- when executing the transition with a Proc' do
it 'should call a Proc on the object with args' do
opts = {:from => 'foo', :to => 'bar', :on_transition => Proc.new {|o| o.test}}
st = AASM::SupportingClasses::StateTransition.new(opts)
args = {:arg1 => '1', :arg2 => '2'}
obj = mock('object')
opts[:on_transition].should_receive(:call).with(any_args)
st.execute(obj, args)
end
it 'should call a Proc on the object without args' do
opts = {:from => 'foo', :to => 'bar', :on_transition => Proc.new {||}}
st = AASM::SupportingClasses::StateTransition.new(opts)
args = {:arg1 => '1', :arg2 => '2'}
obj = mock('object')
opts[:on_transition].should_receive(:call).with(no_args)
st.execute(obj, args)
end
end
describe AASM::SupportingClasses::StateTransition, '- when executing the transition with an :on_transtion method call' do
it 'should accept a String for the method name' do
opts = {:from => 'foo', :to => 'bar', :on_transition => 'test'}
st = AASM::SupportingClasses::StateTransition.new(opts)
args = {:arg1 => '1', :arg2 => '2'}
obj = mock('object')
obj.should_receive(:test)
st.execute(obj, args)
end
it 'should accept a Symbol for the method name' do
opts = {:from => 'foo', :to => 'bar', :on_transition => :test}
st = AASM::SupportingClasses::StateTransition.new(opts)
args = {:arg1 => '1', :arg2 => '2'}
obj = mock('object')
obj.should_receive(:test)
st.execute(obj, args)
end
it 'should pass args if the target method accepts them' do
opts = {:from => 'foo', :to => 'bar', :on_transition => :test}
st = AASM::SupportingClasses::StateTransition.new(opts)
args = {:arg1 => '1', :arg2 => '2'}
obj = mock('object')
obj.class.class_eval do
define_method(:test) {|*args| 'success'}
end
return_value = st.execute(obj, args)
return_value.should == 'success'
end
it 'should NOT pass args if the target method does NOT accept them' do
opts = {:from => 'foo', :to => 'bar', :on_transition => :test}
st = AASM::SupportingClasses::StateTransition.new(opts)
args = {:arg1 => '1', :arg2 => '2'}
obj = mock('object')
obj.class.class_eval do
define_method(:test) {|*args| 'success'}
end
return_value = st.execute(obj, args)
return_value.should == 'success'
end
end