How can I make Rspec fail automatically if a spec doesn't have a should/expect etc

458 views Asked by At

I've found a number of specs in our large codebase (some were even mine :) that forgot to include the .should and passed when they weren't testing anything.

e.g.

describe FooController, behavior_type: :controller do
  before do
    post :create, foo: { bar: 100 }
  end

  it "doesn't really test anything"
    response.body == "{\"result\":\"excellent\"}"
  end
end

This spec will pass regardless of the content of response.body because no RSpec matcher is used.

My question is: is there an RSpec option or a gem or something that would detect when a spec has completed without using any RSpec matchers and without calling pending and raise an error or mark the spec failed or some other status to alert the developer to the fact that the spec isn't doing what they probably think it is?

A followup question would be: shouldn't this be the default behaviour of RSpec? Is there a use case where you'd want to be able to write specs that use no matchers and don't call pending?

FWIW: I know RSpec has a --warnings option which may or may not print a warning about a scenario like this. In practice though for our main project it prints thousands of lines of irrelevant warnings, many of them about third party gems etc.

1

There are 1 answers

4
Peter Alfvin On BEST ANSWER

There is no RSpec option or gem, but there are techniques described in https://github.com/rspec/rspec-core/issues/759, https://github.com/rspec/rspec-core/issues/404 and https://github.com/rspec/rspec-core/issues/740 for doing this yourself.

As for this being the default behavior, the current and past leaders of the RSpec team have argued against it in the aforementioned issue threads.

See issue 404 for a use case for not wanting a failure.