Introduce assert_changes and assert_no_changes#25393
Introduce assert_changes and assert_no_changes#25393rafaelfranca merged 1 commit intorails:masterfrom
assert_changes and assert_no_changes#25393Conversation
|
r? @chancancode (@rails-bot has picked a reviewer for you, use r? to override) |
|
It might be useful to use assert_changes -> { user.token }, to: /\w{24}/ do
put :regenerate_token
end
assert_changes -> {article.published_at}, from: nil, to: Time do
put :publish, params: {id: article.id}
end |
|
👍 for @lsylvester comment. Other than that this seems good to me. @gsamokovarov could you update the PR? |
|
Sure, will look it up in a bit. |
7300ee5 to
02278a3
Compare
|
@rafaelfranca updated the PR with case operator support. Thanks for the idea, @lsylvester! |
Those are assertions that I really do miss from the standard
`ActiveSupport::TestCase`. Think of those as a more general version of
`assert_difference` and `assert_no_difference` (those can be implemented
by assert_changes, should this change be accepted).
Why do we need those? They are useful when you want to check a
side-effect of an operation. `assert_difference` do cover a really
common case, but we `assert_changes` gives us more control. Having a
global error flag? You can test it easily with `assert_changes`. In
fact, you can be really specific about the initial state and the
terminal one.
```ruby
error = Error.new(:bad)
assert_changes -> { Error.current }, from: nil, to: error do
expected_bad_operation
end
```
`assert_changes` follows `assert_difference` and a string can be given
for evaluation as well.
```ruby
error = Error.new(:bad)
assert_changes 'Error.current', from: nil, to: error do
expected_bad_operation
end
```
Check out the test cases if you wanna see more examples.
:beers:
02278a3 to
16f24cd
Compare
|
Is there are a gem that provides similar functionality to the older rails apps? |
|
I can backport it in a gem for sure. |
|
@gsamokovarov would be great to have this methods as separate gem, as I would like to use |
|
It's a new feature, it can't be backported to any other version. I'll get the gem out shortly. |
|
@dmitry add the following in your group :test do
gem 'minitest-assert_changes'
end🍻 |
|
@gsamokovarov thanks for gem! |
Those are assertions that I really do miss from the standard
ActiveSupport::TestCase. Think of those as a more general version ofassert_differenceandassert_no_difference(those can be implementedby assert_changes, should this change be accepted).
Why do we need those? They are useful when you want to check a
side-effect of an operation.
assert_differencedo cover a reallycommon case, but
assert_changesgives us more control. Having aglobal error flag? You can test it easily with
assert_changes. Infact, you can be really specific about the initial state and the
terminal one.
assert_changesfollowsassert_differenceand a string can be givenfor evaluation as well.
Check out the test cases if you wanna see more examples.
🍻