rspec-rails: "expect to receive with" throws error when switching to Rails 7.1

What Ruby, Rails and RSpec versions are you using?

Ruby version: 3.2.1 Rails version: 7.1.1 RSpec version: 3.12 (rspec-rails 6.0.3)

Observed behaviour

Example spec expect(Worker).to receive(:perform_async).with(param1, param2)

returns ArgumentError: wrong number of arguments (given 2, expected 0)

Switching to deprecated Worker.should_receive(:perform_async).with(param1, param2) passes.

Expected behaviour

Passes with “expect to receive with” syntax.

Can you provide an example app?

About this issue

  • Original URL
  • State: closed
  • Created 9 months ago
  • Comments: 17 (5 by maintainers)

Most upvoted comments

Regarding the initial issue, I am getting the same for every with in my specs after upgrading to Rails 7.1:

     ArgumentError:
       wrong number of arguments (given 2, expected 0)
     # /usr/local/bundle/gems/activesupport-7.1.1/lib/active_support/core_ext/object/with.rb:24:in `with'
     # ./spec/lib/mail_configurator_spec.rb:43:in `block (4 levels) in <top (required)>'
     # /usr/local/bundle/gems/webmock-3.14.0/lib/webmock/rspec.rb:37:in `block (2 levels) in <top (required)>'

According to ActiveSupport’s changelog, Object#with was added with 7.1.0.beta1:

  • Add Object#with to set and restore public attributes around a block

    client.timeout # => 5
    client.with(timeout: 1) do
      client.timeout # => 1
    end
    client.timeout # => 5
    

Relevant commit: https://github.com/rails/rails/commit/1884323574b35b3a2286850f3c98f677633728f1

@JonRowe can confirm, 3.12.6 solves the issue, we were still on 3.12.3 👍