Rubocop-rspec: cop for enforcing `should` vs. `expect` with explicit subject

Created on 26 Mar 2018  路  7Comments  路  Source: rubocop-hq/rubocop-rspec

At first, I thought https://github.com/rubocop-rspec/rubocop-rspec/pull/200 also covers the case where are an explicit subject is used such as the one below:

  it "computes shipping cost for a line item" do
    variant = double(:variant, :weight => 10)

    line_item = double(:line_item, :variant => variant, :quantity => 2)

    subject.set_preference(:per_kg, 10)
    subject.compute(line_item).should == 10*2 * 10
  end

then I realized that it is only for implicit subjects. Is there any cop that works in this case too (couldn't find any)? we have quite a big codebase and we want to discourage should syntax.

Most helpful comment

I can give it a try if you want

All 7 comments

Do I understand correctly that you would like to enforce using

expect(subject.compute(line_item)).to eq(10*2 * 10)

instead of

subject.compute(line_item).should == 10*2 * 10

That is a feature of RSpec itself 鈥撀爃ere you can read about how to enable/disable one or the other: https://relishapp.com/rspec/rspec-expectations/docs/syntax-configuration#disable-should-syntax

I personally prefer to configure RSpec to not do any monkey patching at all (no should method on every object, and the outer-most describe or context must be called directly on RSpec): https://relishapp.com/rspec/rspec-core/v/3-7/docs/configuration/zero-monkey-patching-mode#monkey-patched-methods-are-undefined-with-%60disable-monkey-patching!%60

Yep, that's what I meant. I thought I could postpone disabling the syntax and giving Transpec a try by at least stopping new additions of should.

Thanks for the help :ok_hand:

@bquorning: I wonder if we should add a section to the README with something like "Helpful RSpec configuration options" with some things like this that we use and/or a section for explicit non-goals where we think newer versions of RSpec or RSpec configuration already covers the goal and have opted out of implementing it.

That would be helpful and I guess it will reduce the number of issues you need to answer.

That sounds like a good pull request 馃槃

I can give it a try if you want

Fixed by #588.

Was this page helpful?
0 / 5 - 0 ratings