@@ -176,9 +176,69 @@ def test_should_allow_additional_transitions_to_previous_state_in_after_transiti
176
176
assert_equal 'first_gear' , @record . state
177
177
end
178
178
179
- def test_should_return_false_on_before_transition_rollback
180
- @machine . before_transition { raise ActiveRecord ::Rollback }
179
+ def test_should_yield_one_model!
180
+ assert_equal true , @record . save!
181
+ assert_equal 1 , @model . count
182
+ end
181
183
182
- assert_equal false , @record . save
184
+ # explicit tests of #save and #save! to ensure expected behavior
185
+ def test_should_yield_two_models_with_before
186
+ @machine . before_transition { @model . create! }
187
+ assert_equal true , @record . save
188
+ assert_equal 2 , @model . count
183
189
end
190
+
191
+ def test_should_yield_two_models_with_before!
192
+ @machine . before_transition { @model . create! }
193
+ assert_equal true , @record . save!
194
+ assert_equal 2 , @model . count
195
+ end
196
+
197
+ def test_should_raise_on_around_transition_rollback!
198
+ @machine . before_transition { @model . create! }
199
+ @machine . around_transition { @model . create! ; raise ActiveRecord ::Rollback }
200
+
201
+ raised = false
202
+ begin
203
+ @record . save!
204
+ rescue Exception
205
+ raised = true
206
+ end
207
+
208
+ assert_equal true , raised
209
+ assert_equal 0 , @model . count
210
+ end
211
+
212
+ def test_should_return_nil_on_around_transition_rollback
213
+ @machine . before_transition { @model . create! }
214
+ @machine . around_transition { @model . create! ; raise ActiveRecord ::Rollback }
215
+ assert_equal nil , @record . save
216
+ assert_equal 0 , @model . count
217
+ end
218
+
219
+ def test_should_return_nil_on_before_transition_rollback
220
+ @machine . before_transition { raise ActiveRecord ::Rollback }
221
+ assert_equal nil , @record . save
222
+ assert_equal 0 , @model . count
223
+ end
224
+
225
+ #
226
+ # @rosskevin - This fails and I'm not sure why, it was existing behavior.
227
+ # see: https://github.com/state-machines/state_machines-activerecord/pull/26#issuecomment-112911886
228
+ #
229
+ # def test_should_yield_three_models_with_before_and_around_save
230
+ # @machine.before_transition { @model.create!; puts "before ran, now #{@model.count}" }
231
+ # @machine.around_transition { @model.create!; puts "around ran, now #{@model.count}" }
232
+ #
233
+ # assert_equal true, @record.save
234
+ # assert_equal 3, @model.count
235
+ # end
236
+ #
237
+ # def test_should_yield_three_models_with_before_and_around_save!
238
+ # @machine.before_transition { @model.create!; puts "before ran, now #{@model.count}" }
239
+ # @machine.around_transition { @model.create!; puts "around ran, now #{@model.count}" }
240
+ #
241
+ # assert_equal true, @record.save!
242
+ # assert_equal 3, @model.count
243
+ # end
184
244
end
0 commit comments