aasm 3.0.26 → 3.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -17,11 +17,11 @@ describe 'initial states' do
17
17
  let(:bar) {Bar.new}
18
18
 
19
19
  it 'should use the first state defined if no initial state is given' do
20
- bar.aasm.current_state.should == :read
20
+ expect(bar.aasm.current_state).to eq(:read)
21
21
  end
22
22
 
23
23
  it 'should determine initial state from the Proc results' do
24
- Banker.new(Banker::RICH - 1).aasm.current_state.should == :selling_bad_mortgages
25
- Banker.new(Banker::RICH + 1).aasm.current_state.should == :retired
24
+ expect(Banker.new(Banker::RICH - 1).aasm.current_state).to eq(:selling_bad_mortgages)
25
+ expect(Banker.new(Banker::RICH + 1).aasm.current_state).to eq(:retired)
26
26
  end
27
27
  end
@@ -2,16 +2,16 @@ require 'spec_helper'
2
2
 
3
3
  describe 'inspection for common cases' do
4
4
  it 'should support the new DSL' do
5
- Foo.aasm.should respond_to(:states)
6
- Foo.aasm.states.should include(:open)
7
- Foo.aasm.states.should include(:closed)
5
+ expect(Foo.aasm).to respond_to(:states)
6
+ expect(Foo.aasm.states).to include(:open)
7
+ expect(Foo.aasm.states).to include(:closed)
8
8
 
9
- Foo.aasm.should respond_to(:initial_state)
10
- Foo.aasm.initial_state.should == :open
9
+ expect(Foo.aasm).to respond_to(:initial_state)
10
+ expect(Foo.aasm.initial_state).to eq(:open)
11
11
 
12
- Foo.aasm.should respond_to(:events)
13
- Foo.aasm.events.should include(:close)
14
- Foo.aasm.events.should include(:null)
12
+ expect(Foo.aasm).to respond_to(:events)
13
+ expect(Foo.aasm.events).to include(:close)
14
+ expect(Foo.aasm.events).to include(:null)
15
15
  end
16
16
 
17
17
  context "instance level inspection" do
@@ -20,77 +20,77 @@ describe 'inspection for common cases' do
20
20
 
21
21
  it "delivers all states" do
22
22
  states = foo.aasm.states
23
- states.should include(:open)
24
- states.should include(:closed)
23
+ expect(states).to include(:open)
24
+ expect(states).to include(:closed)
25
25
 
26
26
  states = foo.aasm.states(:permissible => true)
27
- states.should include(:closed)
28
- states.should_not include(:open)
27
+ expect(states).to include(:closed)
28
+ expect(states).not_to include(:open)
29
29
 
30
30
  foo.close
31
- foo.aasm.states(:permissible => true).should be_empty
31
+ expect(foo.aasm.states(:permissible => true)).to be_empty
32
32
  end
33
33
 
34
34
  it "delivers all states for subclasses" do
35
35
  states = two.aasm.states
36
- states.should include(:open)
37
- states.should include(:closed)
38
- states.should include(:foo)
36
+ expect(states).to include(:open)
37
+ expect(states).to include(:closed)
38
+ expect(states).to include(:foo)
39
39
 
40
40
  states = two.aasm.states(:permissible => true)
41
- states.should include(:closed)
42
- states.should_not include(:open)
41
+ expect(states).to include(:closed)
42
+ expect(states).not_to include(:open)
43
43
 
44
44
  two.close
45
- two.aasm.states(:permissible => true).should be_empty
45
+ expect(two.aasm.states(:permissible => true)).to be_empty
46
46
  end
47
47
 
48
48
  it "delivers all events" do
49
49
  events = foo.aasm.events
50
- events.should include(:close)
51
- events.should include(:null)
50
+ expect(events).to include(:close)
51
+ expect(events).to include(:null)
52
52
  foo.close
53
- foo.aasm.events.should be_empty
53
+ expect(foo.aasm.events).to be_empty
54
54
  end
55
55
  end
56
56
 
57
57
  it 'should list states in the order they have been defined' do
58
- Conversation.aasm.states.should == [:needs_attention, :read, :closed, :awaiting_response, :junk]
58
+ expect(Conversation.aasm.states).to eq([:needs_attention, :read, :closed, :awaiting_response, :junk])
59
59
  end
60
60
  end
61
61
 
62
62
  describe "special cases" do
63
63
  it "should support valid a state name" do
64
- Argument.aasm.states.should include(:invalid)
65
- Argument.aasm.states.should include(:valid)
64
+ expect(Argument.aasm.states).to include(:invalid)
65
+ expect(Argument.aasm.states).to include(:valid)
66
66
 
67
67
  argument = Argument.new
68
- argument.invalid?.should be_true
69
- argument.aasm.current_state.should == :invalid
68
+ expect(argument.invalid?).to be_true
69
+ expect(argument.aasm.current_state).to eq(:invalid)
70
70
 
71
71
  argument.valid!
72
- argument.valid?.should be_true
73
- argument.aasm.current_state.should == :valid
72
+ expect(argument.valid?).to be_true
73
+ expect(argument.aasm.current_state).to eq(:valid)
74
74
  end
75
75
  end
76
76
 
77
77
  describe 'aasm.states_for_select' do
78
78
  it "should return a select friendly array of states" do
79
- Foo.aasm.should respond_to(:states_for_select)
80
- Foo.aasm.states_for_select.should == [['Open', 'open'], ['Closed', 'closed']]
79
+ expect(Foo.aasm).to respond_to(:states_for_select)
80
+ expect(Foo.aasm.states_for_select).to eq([['Open', 'open'], ['Closed', 'closed']])
81
81
  end
82
82
  end
83
83
 
84
84
  describe 'aasm.from_states_for_state' do
85
85
  it "should return all from states for a state" do
86
- AuthMachine.aasm.should respond_to(:from_states_for_state)
86
+ expect(AuthMachine.aasm).to respond_to(:from_states_for_state)
87
87
  froms = AuthMachine.aasm.from_states_for_state(:active)
88
- [:pending, :passive, :suspended].each {|from| froms.should include(from)}
88
+ [:pending, :passive, :suspended].each {|from| expect(froms).to include(from)}
89
89
  end
90
90
 
91
91
  it "should return from states for a state for a particular transition only" do
92
92
  froms = AuthMachine.aasm.from_states_for_state(:active, :transition => :unsuspend)
93
- [:suspended].each {|from| froms.should include(from)}
93
+ [:suspended].each {|from| expect(froms).to include(from)}
94
94
  end
95
95
  end
96
96
 
@@ -98,7 +98,7 @@ describe 'permissible events' do
98
98
  let(:foo) {Foo.new}
99
99
 
100
100
  it 'work' do
101
- foo.aasm.permissible_events.should include(:close)
102
- foo.aasm.permissible_events.should_not include(:null)
101
+ expect(foo.aasm.permissible_events).to include(:close)
102
+ expect(foo.aasm.permissible_events).not_to include(:null)
103
103
  end
104
104
  end
@@ -31,14 +31,14 @@ describe 'localized state names' do
31
31
 
32
32
  it 'should localize' do
33
33
  state = LocalizerTestModel.aasm.states.detect {|s| s == :opened}
34
- state.localized_name.should == "It's open now!"
35
- state.human_name.should == "It's open now!"
34
+ expect(state.localized_name).to eq("It's open now!")
35
+ expect(state.human_name).to eq("It's open now!")
36
36
  end
37
37
 
38
38
  it 'should use fallback' do
39
39
  state = LocalizerTestModel.aasm.states.detect {|s| s == :closed}
40
- state.localized_name.should == 'Closed'
41
- state.human_name.should == 'Closed'
40
+ expect(state.localized_name).to eq('Closed')
41
+ expect(state.human_name).to eq('Closed')
42
42
  end
43
43
  end
44
44
 
@@ -58,21 +58,21 @@ describe AASM::Localizer, "new style" do
58
58
 
59
59
  context 'aasm.human_state' do
60
60
  it 'should return translated state value' do
61
- foo_opened.aasm.human_state.should == "It's open now!"
61
+ expect(foo_opened.aasm.human_state).to eq("It's open now!")
62
62
  end
63
63
 
64
64
  it 'should return humanized value if not localized' do
65
- foo_closed.aasm.human_state.should == "Closed"
65
+ expect(foo_closed.aasm.human_state).to eq("Closed")
66
66
  end
67
67
  end
68
68
 
69
69
  context 'aasm_human_event_name' do
70
70
  it 'should return translated event name' do
71
- LocalizerTestModel.aasm_human_event_name(:close).should == "Let's close it!"
71
+ expect(LocalizerTestModel.aasm_human_event_name(:close)).to eq("Let's close it!")
72
72
  end
73
73
 
74
74
  it 'should return humanized event name' do
75
- LocalizerTestModel.aasm_human_event_name(:open).should == "Open"
75
+ expect(LocalizerTestModel.aasm_human_event_name(:open)).to eq("Open")
76
76
  end
77
77
  end
78
78
  end
@@ -93,21 +93,21 @@ describe AASM::Localizer, "deprecated style" do
93
93
 
94
94
  context 'aasm.human_state' do
95
95
  it 'should return translated state value' do
96
- foo_opened.aasm.human_state.should == "It's open now!"
96
+ expect(foo_opened.aasm.human_state).to eq("It's open now!")
97
97
  end
98
98
 
99
99
  it 'should return humanized value if not localized' do
100
- foo_closed.aasm.human_state.should == "Closed"
100
+ expect(foo_closed.aasm.human_state).to eq("Closed")
101
101
  end
102
102
  end
103
103
 
104
104
  context 'aasm_human_event_name' do
105
105
  it 'should return translated event name' do
106
- LocalizerTestModel.aasm_human_event_name(:close).should == "Let's close it!"
106
+ expect(LocalizerTestModel.aasm_human_event_name(:close)).to eq("Let's close it!")
107
107
  end
108
108
 
109
109
  it 'should return humanized event name' do
110
- LocalizerTestModel.aasm_human_event_name(:open).should == "Open"
110
+ expect(LocalizerTestModel.aasm_human_event_name(:open)).to eq("Open")
111
111
  end
112
112
  end
113
113
  end
@@ -5,8 +5,8 @@ describe "the new dsl" do
5
5
  let(:process) {ProcessWithNewDsl.new}
6
6
 
7
7
  it 'should not conflict with other event or state methods' do
8
- lambda {ProcessWithNewDsl.state}.should raise_error(RuntimeError, "wrong state method")
9
- lambda {ProcessWithNewDsl.event}.should raise_error(RuntimeError, "wrong event method")
8
+ expect {ProcessWithNewDsl.state}.to raise_error(RuntimeError, "wrong state method")
9
+ expect {ProcessWithNewDsl.event}.to raise_error(RuntimeError, "wrong event method")
10
10
  end
11
11
 
12
12
  end
@@ -9,8 +9,8 @@ load_schema
9
9
 
10
10
  shared_examples_for "aasm model" do
11
11
  it "should include persistence mixins" do
12
- klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence)
13
- klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence::InstanceMethods)
12
+ expect(klass.included_modules).to be_include(AASM::Persistence::ActiveRecordPersistence)
13
+ expect(klass.included_modules).to be_include(AASM::Persistence::ActiveRecordPersistence::InstanceMethods)
14
14
  end
15
15
  end
16
16
 
@@ -18,45 +18,45 @@ describe "instance methods" do
18
18
  let(:gate) {Gate.new}
19
19
 
20
20
  it "should respond to aasm persistence methods" do
21
- gate.should respond_to(:aasm_read_state)
22
- gate.should respond_to(:aasm_write_state)
23
- gate.should respond_to(:aasm_write_state_without_persistence)
21
+ expect(gate).to respond_to(:aasm_read_state)
22
+ expect(gate).to respond_to(:aasm_write_state)
23
+ expect(gate).to respond_to(:aasm_write_state_without_persistence)
24
24
  end
25
25
 
26
26
  it "should return the initial state when new and the aasm field is nil" do
27
- gate.aasm.current_state.should == :opened
27
+ expect(gate.aasm.current_state).to eq(:opened)
28
28
  end
29
29
 
30
30
  it "should return the aasm column when new and the aasm field is not nil" do
31
31
  gate.aasm_state = "closed"
32
- gate.aasm.current_state.should == :closed
32
+ expect(gate.aasm.current_state).to eq(:closed)
33
33
  end
34
34
 
35
35
  it "should return the aasm column when not new and the aasm_column is not nil" do
36
- gate.stub(:new_record?).and_return(false)
36
+ allow(gate).to receive(:new_record?).and_return(false)
37
37
  gate.aasm_state = "state"
38
- gate.aasm.current_state.should == :state
38
+ expect(gate.aasm.current_state).to eq(:state)
39
39
  end
40
40
 
41
41
  it "should allow a nil state" do
42
- gate.stub(:new_record?).and_return(false)
42
+ allow(gate).to receive(:new_record?).and_return(false)
43
43
  gate.aasm_state = nil
44
- gate.aasm.current_state.should be_nil
44
+ expect(gate.aasm.current_state).to be_nil
45
45
  end
46
46
 
47
47
  it "should call aasm_ensure_initial_state on validation before create" do
48
- gate.should_receive(:aasm_ensure_initial_state).and_return(true)
48
+ expect(gate).to receive(:aasm_ensure_initial_state).and_return(true)
49
49
  gate.valid?
50
50
  end
51
51
 
52
52
  it "should call aasm_ensure_initial_state before create, even if skipping validations" do
53
- gate.should_receive(:aasm_ensure_initial_state).and_return(true)
53
+ expect(gate).to receive(:aasm_ensure_initial_state).and_return(true)
54
54
  gate.save(:validate => false)
55
55
  end
56
56
 
57
57
  it "should not call aasm_ensure_initial_state on validation before update" do
58
- gate.stub(:new_record?).and_return(false)
59
- gate.should_not_receive(:aasm_ensure_initial_state)
58
+ allow(gate).to receive(:new_record?).and_return(false)
59
+ expect(gate).not_to receive(:aasm_ensure_initial_state)
60
60
  gate.valid?
61
61
  end
62
62
 
@@ -64,36 +64,36 @@ end
64
64
 
65
65
  describe 'subclasses' do
66
66
  it "should have the same states as its parent class" do
67
- DerivateNewDsl.aasm.states.should == SimpleNewDsl.aasm.states
67
+ expect(DerivateNewDsl.aasm.states).to eq(SimpleNewDsl.aasm.states)
68
68
  end
69
69
 
70
70
  it "should have the same events as its parent class" do
71
- DerivateNewDsl.aasm.events.should == SimpleNewDsl.aasm.events
71
+ expect(DerivateNewDsl.aasm.events).to eq(SimpleNewDsl.aasm.events)
72
72
  end
73
73
 
74
74
  it "should have the same column as its parent even for the new dsl" do
75
- SimpleNewDsl.aasm_column.should == :status
76
- DerivateNewDsl.aasm_column.should == :status
75
+ expect(SimpleNewDsl.aasm_column).to eq(:status)
76
+ expect(DerivateNewDsl.aasm_column).to eq(:status)
77
77
  end
78
78
  end
79
79
 
80
80
  describe "named scopes with the new DSL" do
81
81
  context "Does not already respond_to? the scope name" do
82
82
  it "should add a scope" do
83
- SimpleNewDsl.should respond_to(:unknown_scope)
84
- SimpleNewDsl.unknown_scope.is_a?(ActiveRecord::Relation).should be_true
83
+ expect(SimpleNewDsl).to respond_to(:unknown_scope)
84
+ expect(SimpleNewDsl.unknown_scope.is_a?(ActiveRecord::Relation)).to be_true
85
85
  end
86
86
  end
87
87
 
88
88
  context "Already respond_to? the scope name" do
89
89
  it "should not add a scope" do
90
- SimpleNewDsl.should respond_to(:new)
91
- SimpleNewDsl.new.class.should == SimpleNewDsl
90
+ expect(SimpleNewDsl).to respond_to(:new)
91
+ expect(SimpleNewDsl.new.class).to eq(SimpleNewDsl)
92
92
  end
93
93
  end
94
94
 
95
95
  it "does not create scopes if requested" do
96
- NoScope.should_not respond_to(:ignored_scope)
96
+ expect(NoScope).not_to respond_to(:ignored_scope)
97
97
  end
98
98
 
99
99
  end
@@ -101,8 +101,8 @@ end
101
101
  describe 'initial states' do
102
102
 
103
103
  it 'should support conditions' do
104
- Thief.new(:skilled => true).aasm.current_state.should == :rich
105
- Thief.new(:skilled => false).aasm.current_state.should == :jailed
104
+ expect(Thief.new(:skilled => true).aasm.current_state).to eq(:rich)
105
+ expect(Thief.new(:skilled => false).aasm.current_state).to eq(:jailed)
106
106
  end
107
107
  end
108
108
 
@@ -110,54 +110,54 @@ describe 'transitions with persistence' do
110
110
 
111
111
  it "should work for valid models" do
112
112
  valid_object = Validator.create(:name => 'name')
113
- valid_object.should be_sleeping
113
+ expect(valid_object).to be_sleeping
114
114
  valid_object.status = :running
115
- valid_object.should be_running
115
+ expect(valid_object).to be_running
116
116
  end
117
117
 
118
118
  it 'should not store states for invalid models' do
119
119
  validator = Validator.create(:name => 'name')
120
- validator.should be_valid
121
- validator.should be_sleeping
120
+ expect(validator).to be_valid
121
+ expect(validator).to be_sleeping
122
122
 
123
123
  validator.name = nil
124
- validator.should_not be_valid
125
- validator.run!.should be_false
126
- validator.should be_sleeping
124
+ expect(validator).not_to be_valid
125
+ expect(validator.run!).to be_false
126
+ expect(validator).to be_sleeping
127
127
 
128
128
  validator.reload
129
- validator.should_not be_running
130
- validator.should be_sleeping
129
+ expect(validator).not_to be_running
130
+ expect(validator).to be_sleeping
131
131
 
132
132
  validator.name = 'another name'
133
- validator.should be_valid
134
- validator.run!.should be_true
135
- validator.should be_running
133
+ expect(validator).to be_valid
134
+ expect(validator.run!).to be_true
135
+ expect(validator).to be_running
136
136
 
137
137
  validator.reload
138
- validator.should be_running
139
- validator.should_not be_sleeping
138
+ expect(validator).to be_running
139
+ expect(validator).not_to be_sleeping
140
140
  end
141
141
 
142
142
  it 'should store states for invalid models if configured' do
143
143
  persistor = InvalidPersistor.create(:name => 'name')
144
- persistor.should be_valid
145
- persistor.should be_sleeping
144
+ expect(persistor).to be_valid
145
+ expect(persistor).to be_sleeping
146
146
 
147
147
  persistor.name = nil
148
- persistor.should_not be_valid
149
- persistor.run!.should be_true
150
- persistor.should be_running
148
+ expect(persistor).not_to be_valid
149
+ expect(persistor.run!).to be_true
150
+ expect(persistor).to be_running
151
151
 
152
152
  persistor = InvalidPersistor.find(persistor.id)
153
153
  persistor.valid?
154
- persistor.should be_valid
155
- persistor.should be_running
156
- persistor.should_not be_sleeping
154
+ expect(persistor).to be_valid
155
+ expect(persistor).to be_running
156
+ expect(persistor).not_to be_sleeping
157
157
 
158
158
  persistor.reload
159
- persistor.should be_running
160
- persistor.should_not be_sleeping
159
+ expect(persistor).to be_running
160
+ expect(persistor).not_to be_sleeping
161
161
  end
162
162
 
163
163
  describe 'transactions' do
@@ -165,41 +165,81 @@ describe 'transitions with persistence' do
165
165
  let(:transactor) { Transactor.create!(:name => 'transactor', :worker => worker) }
166
166
 
167
167
  it 'should rollback all changes' do
168
- transactor.should be_sleeping
169
- worker.status.should == 'sleeping'
168
+ expect(transactor).to be_sleeping
169
+ expect(worker.status).to eq('sleeping')
170
170
 
171
- lambda {transactor.run!}.should raise_error(StandardError, 'failed on purpose')
172
- transactor.should be_running
173
- worker.reload.status.should == 'sleeping'
171
+ expect {transactor.run!}.to raise_error(StandardError, 'failed on purpose')
172
+ expect(transactor).to be_running
173
+ expect(worker.reload.status).to eq('sleeping')
174
174
  end
175
175
 
176
- it "should rollback all changes in nested transaction" do
177
- transactor.should be_sleeping
178
- worker.status.should == 'sleeping'
176
+ context "nested transactions" do
177
+ it "should rollback all changes in nested transaction" do
178
+ expect(transactor).to be_sleeping
179
+ expect(worker.status).to eq('sleeping')
179
180
 
180
- Worker.transaction do
181
- lambda { transactor.run! }.should raise_error(StandardError, 'failed on purpose')
181
+ Worker.transaction do
182
+ expect { transactor.run! }.to raise_error(StandardError, 'failed on purpose')
183
+ end
184
+
185
+ expect(transactor).to be_running
186
+ expect(worker.reload.status).to eq('sleeping')
182
187
  end
183
188
 
184
- transactor.should be_running
185
- worker.reload.status.should == 'sleeping'
189
+ it "should only rollback changes in the main transaction not the nested one" do
190
+ # change configuration to not require new transaction
191
+ AASM::StateMachine[Transactor].config.requires_new_transaction = false
192
+
193
+ expect(transactor).to be_sleeping
194
+ expect(worker.status).to eq('sleeping')
195
+
196
+ Worker.transaction do
197
+ expect { transactor.run! }.to raise_error(StandardError, 'failed on purpose')
198
+ end
199
+
200
+ expect(transactor).to be_running
201
+ expect(worker.reload.status).to eq('running')
202
+ end
186
203
  end
187
204
 
188
205
  describe "after_commit callback" do
189
206
  it "should fire :after_commit if transaction was successful" do
190
207
  validator = Validator.create(:name => 'name')
191
- validator.should be_sleeping
208
+ expect(validator).to be_sleeping
192
209
  validator.run!
193
- validator.should be_running
194
- validator.name.should_not == "name"
210
+ expect(validator).to be_running
211
+ expect(validator.name).not_to eq("name")
195
212
  end
196
213
 
197
214
  it "should not fire :after_commit if transaction failed" do
198
215
  validator = Validator.create(:name => 'name')
199
- lambda { validator.fail! }.should raise_error(StandardError, 'failed on purpose')
200
- validator.name.should == "name"
216
+ expect { validator.fail! }.to raise_error(StandardError, 'failed on purpose')
217
+ expect(validator.name).to eq("name")
201
218
  end
202
219
 
203
220
  end
204
221
  end
205
222
  end
223
+
224
+ describe "invalid states with persistence" do
225
+
226
+ it "should not store states" do
227
+ validator = Validator.create(:name => 'name')
228
+ validator.status = 'invalid_state'
229
+ expect(validator.save).to be_false
230
+ expect {validator.save!}.to raise_error(ActiveRecord::RecordInvalid)
231
+
232
+ validator.reload
233
+ expect(validator).to be_sleeping
234
+ end
235
+
236
+ it "should store invalid states if configured" do
237
+ persistor = InvalidPersistor.create(:name => 'name')
238
+ persistor.status = 'invalid_state'
239
+ expect(persistor.save).to be_true
240
+
241
+ persistor.reload
242
+ expect(persistor.status).to eq('invalid_state')
243
+ end
244
+
245
+ end