David West
David West

Reputation: 2328

Is there a way to make RSpec fail if there is no assertion?

Minitest has proveit!

Does RSpec have a similar way to require assertions?

Upvotes: 0

Views: 91

Answers (2)

asok
asok

Reputation: 328

This is my take:

RSpec.configure do |config|
  config.include(Module.new do
                   attr_writer :expectation_set_count

                   def expectation_set_count
                     @expectation_set_count ||= 0
                   end

                   def expect(*)
                     self.expectation_set_count += 1

                     super
                   end
                 end)

  config.after do
    expect(expectation_set_count).to be > 0
  end
end

Upvotes: 1

Greg
Greg

Reputation: 6628

You can configure the RSpec for that, check this out:

https://github.com/rspec/rspec-core/issues/404#issuecomment-11431199 and http://blog.sorah.jp/2012/12/17/rspec-warn-for-no-expectations

Basically you need to set up after hook and check the metadata if there was an expectaiton run: (copy of the code in case the url goes down)

RSpec.configure do |config|
  config.after(:each) do
    result = self.example.metadata[:execution_result]
    has_mock_expectations = RSpec::Mocks.space.instance_eval{receivers}.empty?
    if !result[:exception] && !result[:pending_message] && !RSpec::Matchers.last_should && hasnt_mock_expectations
      $stderr.puts "[WARN] No expectations found in example at #{self.example.location}: Maybe you forgot to write `should` in the example?"
    end
  end
end

Upvotes: 0

Related Questions