-
-
Notifications
You must be signed in to change notification settings - Fork 117
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
Recommend preferring helper methods to hooks #70
Comments
Both of these are not ideal. Given the provided examples, |
If it's not clear what we're testing, then |
As we've discussed previously, No doubt that it doesn't make much sense to extract something if it is not reused. I imagine something like:
Agree that original examples are confusing, thanks for bringing this up. PS Fixed. Please take another look. |
I'm not sure how strong the motivating case is. I think switching from a In this case (assuming this is forced by something like ActiveRecord) i think the assignment after mutation is part of what's awkward. If you had an article that was initialized like let(:article) do
Article.new.tap do |article|
article.author = author
end
end or just inline it entirely if it's not many times. I think helper methods are good sometimes, but in this case it feels equally overkill-ish to me as the As a note, the |
This recommendation is taken from an Effective Testing with RSpec book. I tend to agree that "recommend preferring helper methods to hooks" is way too strong compared to something like:
and
WDYT about those two? |
Sure, I agree with those statements. I think a realistic motivating example is probably pretty hard to construct since this is more for non-trivial tests, but I agree with and follow that general advice in various real-world situations. |
I commented over in #56, but I would actually advocate for more strongly saying that In this case, allowing for both patterns brings up several questions while I'm updating tests that I have to spend time considering: Is it time to refactor this to the other pattern? Do I have the time to do it now? Should I add it as part of this change or make a separate PR? And when I do refactor it, it likely leads to a sweeping refactor across the tests where I have to now add references to the new helper functions. And that's all if I even remember to think about it. I'd rather a slightly more verbose format in all cases in order to make tests consistent, and easy to understand and modify individually. |
I completely agree about your point of consistency in the scope of a given project.
WDYT of adding a similar note to this guide? |
On a different note, there clearly seems to be no single way of doing things.
I'm more inclined to add some options with soft wording:
and
that implies no pressure and still allows to use |
@pirj I think what you've said here makes sense. I'd probably still prefer stronger wording against using |
As a note for here and #56, I can certainly respect wanting to enforce consistent practice and I find several arguments about using or overusing Just my $0.02. |
Don't overuse hooks, prefer explicitness.
Helper methods are more explicit and even provide more flexibility
An example off the top of my head:
Just like with #56, this should be used when there's a compelling benefit for extraction - improved readability or code reuse.
The text was updated successfully, but these errors were encountered: