Skip to content
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

Support hash as first argument in assert_difference. #31600

Merged
merged 3 commits into from
Jan 18, 2018

Conversation

julien-meichelbeck
Copy link
Contributor

@julien-meichelbeck julien-meichelbeck commented Dec 30, 2017

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.

assert_difference 'Article.count' => 1, 'Notification.count' => 2 do
  # post :create, params: { article: {...} }
end

Other Information

Implementation: I also removed the each_with_indexwhich seemed redundant with the usage of the zip method.

@rails-bot
Copy link

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.

@sgrif
Copy link
Contributor

sgrif commented Jan 12, 2018

/cc @rafaelfranca @dhh I like this change, but I think this needs sign-off from a core team member

r? @rafaelfranca

@rails-bot rails-bot assigned rafaelfranca and unassigned sgrif Jan 12, 2018
@dhh
Copy link
Member

dhh commented Jan 13, 2018

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.

@julien-meichelbeck
Copy link
Contributor Author

julien-meichelbeck commented Jan 13, 2018

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.

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 ActiveRecord_Relation.

99% of the time when I'm using assert_difference it's on models, why should I bother with string-eval and procs when I could just write this:

# 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.

@dhh
Copy link
Member

dhh commented Jan 13, 2018

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!

@eugeneius
Copy link
Member

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
@julien-meichelbeck
Copy link
Contributor Author

@eugeneius Thanks, done !

@eugeneius
Copy link
Member

Nice one 👍

@@ -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`
Copy link
Member

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure!

@rafaelfranca rafaelfranca merged commit e0f0d71 into rails:master Jan 18, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants