Open
Description
Don't overuse hooks, prefer explicitness.
Helper methods are more explicit and even provide more flexibility
An example off the top of my head:
# bad
let(:article) { Article.new }
before do
article.author = author
end
context 'with an author' do
let(:author) { Author.new('John') }
it "returns article's author name" do
expect(article.author_name).to eq 'John'
end
end
context 'without an author' do
let(:author) { nil }
it "returns a placeholder" do
expect(article.author_name).to eq 'Unknown'
end
end
# good
def article_for_author(author)
article = Article.new
article.author = author
article
end
context 'with an author' do
let(:author) { Author.new('John') }
it "returns article's author name" do
article = article_for_author(author)
expect(article.author_name).to eq 'John'
end
end
context 'without an author' do
it "returns a placeholder" do
article = article_for_author(nil)
expect(article.author_name).to eq 'Unknown'
end
end
Just like with #56, this should be used when there's a compelling benefit for extraction - improved readability or code reuse.
Metadata
Metadata
Assignees
Labels
No labels