rspec-mocks: 'receive' matcher doesn't support custom failure messages

Subject of the issue

rspec-expectations supports the passing of customized failure messages to the #to method:

expect(array).to be_empty, "expected empty array, got #{array.inspect}"

The documentation states that

This works for any matcher other than the operator matchers.

This does not work for the receive matcher, however, and there’s no mention of it in the rspec-mocks documentation.

Your environment

  • Ruby version: 2.4.4
  • rspec-mocks version: 3.8.0

Steps to reproduce

RSpec.describe RSpec do
  # rspec outputs this custom failure message
  it 'supports custom failure messages for base matchers' do
    expect(1).to eq(2), 'something went wrong!'
  end

  # rspec does NOT output this custom failure message
  it 'supports custom failure messages for rspec-mocks matchers' do
    expect(RSpec).to receive(:describe), 'something went wrong!'
  end
end

Expected behavior

I expect the second example to output the custom message, "something went wrong!"

Actual behavior

The default failure message is output instead:

(RSpec).describe(*(any args))
    expected: 1 time with any arguments
    received: 0 times with any arguments

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 7
  • Comments: 21 (17 by maintainers)

Most upvoted comments

Hello

I did some pairing this morning with @nicoolas25 on this issue. We wrote draft patchs:

With:

require 'spec_helper'

RSpec.describe "custom message" do
  it 'display custom mock message' do
    expect(1).to eq(3), 'CUSTOM eq message'
  end

  it do
    expect(RSpec).to receive(:describe), 'CUSTOM receive message'
  end
end

It produces:

$ rspec  
FF

Failures:

  1) custom message display custom mock message
     Failure/Error: expect(1).to eq(3), 'CUSTOM eq message'
       CUSTOM eq message
     # ./spec/repro_spec.rb:5:in `block (2 levels) in <top (required)>'

  2) custom message is expected to receive describe(*(any args)) 1 time
     Failure/Error: expect(RSpec).to receive(:describe), 'CUSTOM receive message'
       CUSTOM receive message
     # ./spec/repro_spec.rb:9:in `block (2 levels) in <top (required)>'

Finished in 0.02302 seconds (files took 0.1084 seconds to load)
2 examples, 2 failures

Failed examples:

rspec ./spec/repro_spec.rb:4 # custom message display custom mock message
rspec ./spec/repro_spec.rb:8 # custom message is expected to receive describe(*(any args)) 1 time

Is something like that you were thinking @JonRowe in https://github.com/rspec/rspec-mocks/issues/1250#issuecomment-453199176?

We try to do minimal changes but the result in https://github.com/rspec/rspec-expectations/pull/1156 looks maybe a little bit “hacky”.

Thanks in advance for the review. I will add tests when the idea will be validated.