-
Notifications
You must be signed in to change notification settings - Fork 21.8k
Support hash as first argument in assert_difference
.
#31600
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Thanks for the pull request, and welcome! The Rails team is excited to review your changes, and you should hear from @sgrif (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. This repository is being automatically checked for code quality issues using Code Climate. You can see results for this analysis in the PR status below. Newly introduced issues should be fixed before a Pull Request is considered ready to review. Please see the contribution instructions for more information. |
e2d0486
to
088b3b3
Compare
/cc @rafaelfranca @dhh I like this change, but I think this needs sign-off from a core team member |
I like the option to be able to declare multiple tests, but I’m not so keen on encouraging the string-eval approach further. I’d say that’s a deprecated form. If we have a way of letting the expression take multiple procs, then that’s probably the way to go instead. |
088b3b3
to
af68b1f
Compare
It also works with multiple procs (I just pushed a testcase). @dhh Regarding the deprecation of the string-eval approach, I couldn't agree more and I was planning on submitting a third approach in a future pull-request which would be 99% of the time when I'm using # Array
assert_difference [Article, Post.published], 2 do
end
# Hash
assert_difference Article => 1, Post.published => 2, Notification.sent => 4 do
end Anyway, I'm going off-topic, just wanted to know if you guys like this idea. |
Looks good with the Proc approach. Let’s highlight that in the change log and the docs. Not loving the straight scope approach. Too much has to be inferred from that re: count. Thanks for working on this! |
47b3c7d
to
de3ad8e
Compare
Passing a custom error message seems to be broken here - I would expect this to work: assert_difference({ ->{ Article.count } => 1, ->{ Post.count } => 2 }, "an article and two posts should be created") do
# ...
end |
This allows to specify multiple numeric differences in the same assertion. Example: assert_difference 'Article.count' => 1, 'Notification.count' => 2 do # post :create, params: { article: {...} } end
de3ad8e
to
1b4c1c0
Compare
@eugeneius Thanks, done ! |
Nice one 👍 |
activesupport/CHANGELOG.md
Outdated
@@ -1,3 +1,9 @@ | |||
* Support hash as first argument in `assert_difference`. This allows to specify multiple | |||
numeric differences in the same assertion. | |||
`assert_difference ->{ Article.count } => 1, ->{ Post.count } => 2` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you give this code example its own paragraph and indent it with four spaces, instead of wrapping it in backticks?
http://guides.rubyonrails.org/contributing_to_ruby_on_rails.html#updating-the-changelog
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure!
Summary
assert_difference
already supports multiple expressions but only when the expected difference is the same for all of them (assert_difference [ 'Article.count', 'Post.count' ], 2
).This pull-request allows to specify multiple numeric differences in the same assertion which is also very convenient to avoid deep block nesting.
Other Information
Implementation: I also removed the
each_with_index
which seemed redundant with the usage of thezip
method.