rubocop / rspec-style-guide

Best practices for writing your specs!
http://rspec.rubystyle.guide
958 stars 118 forks source link

`let` and `let!` to be declared before `before` and `after` blocks? #134

Closed schinery closed 1 year ago

schinery commented 1 year ago

In many of the examples, such as Let Grouping and Leading Subject, it implies that the spec structure should be:

describe Article do
  subject { FactoryBot.create(:some_article) }
  let(:user) { FactoryBot.create(:user) }

  before do
    # ...
  end

  describe '#summary' do
    # ...
  end
end

where the let is declared before the before block. Is this a correct assumption and should it be in the style guide?

# Bad
describe Article do
  subject { FactoryBot.create(:some_article) }

  before do
    # ...
  end

  let(:user) { FactoryBot.create(:user) }

  describe '#summary' do
    # ...
  end
end

# Good
describe Article do
  subject { FactoryBot.create(:some_article) }
  let(:user) { FactoryBot.create(:user) }

  before do
    # ...
  end

  describe '#summary' do
    # ...
  end
end
pirj commented 1 year ago

It’s a common practice. A pr is welcome.