I don't know of a way to make
receive fail eagerly. However,
have_received fails eagerly, so if it is the first of several expectations it will be the one that fails the test and that RSpec reports.
class Foo def self.bar(baz) end end describe "RSpec" do it "reports a non-mock expectation failure before a mock expectation failure" do expect(Foo).to receive(:bar).with(1) expect(true).to be_falsy # RSpec reports this failure Foo.bar 2 end it "reports a spy expectation failure when you'd expect it to be reported" do allow(Foo).to receive(:bar) # Spy on Foo Foo.bar 2 expect(Foo).to have_received(:bar).with(1) # RSpec reports this failure expect(true).to be_falsy end end
See the documentation of RSpec spies for details.
In addition to being a solution to your problem, spies also provide the more logical arrange-act-assert sequence. Whether you prefer this solution or Adarsh's
aggregate_failures solution depends partly on how much it bugs you to have to set up the spy with
allow as well as writing the expectation, and also whether the second expectation (the one that was masking the mock expectation failure) has any meaning if the mock expectation fails or is just noise in that case.