16

I'm trying to stub:

Thing.where(uuid: options['uuid']).first

Via:

allow(Thing).to receive_message_chain(:where, :first)
  .with(uuid: thing_double.uuid)
  .and_return(nil)

But this is returning:

 #<Double (anonymous)> received :first with unexpected arguments
         expected: ({:uuid=>123})
              got: (no args)

Is there a different way I should be validating arguments for message chains?

1
  • Wouldn't it be written allow(Thing).to receive_message_chain(:where).first?
    – MageeWorld
    Commented Jul 12, 2016 at 18:51

3 Answers 3

14

It doesn't appear that you can use with in combination with receive_message_chain when the arguments pertain anything other than the final method. Thus the message:

#<Double (anonymous)> received :first with unexpected arguments

This makes sense -- how can RSpec know which method in the chain should receive the arguments?

To verify the argument expectation, don't stub the chain, just stub where

allow(Thing).to receive(:where).with(uuid: 1).and_return([])
expect(Thing.where(uuid: 1).first).to eq nil

Or omit the arguments:

 allow(Thing).to receive_message_chain(:where, :first).and_return(nil)
 expect(Thing.where(uuid: 1).first).to eq nil

receive_message_chain is not recommended IMO. From the docs:

you should consider any use of receive_message_chain a code smell

9

Here's how we addresses a similar situation:

expect(Theme).to receive(:scoped).and_return(double('scope').tap do |scope| 
  expect(scope).to receive(:includes).with(:categories).and_return scope
  expect(scope).to receive(:where).with(categories: { parent_id: '1'}).and_return scope
  expect(scope).to receive(:order).with('themes.updated_at DESC').and_return scope
  expect(scope).to receive(:offset).with(10).and_return scope
  expect(scope).to receive(:limit).with(10000)
end)

In recent months, by pure accident, I discovered that you can actually chain your "with" invocation in the order of the message chain. Thus, the previous example, becomes this:

expect(Theme).to receive_message_chain(:scoped, :includes, :where, :order, :offset, :limit).with(no_args).with(:categories).with(categories: { parent_id: '1'}).with('themes.updated_at DESC').with(10).with(10000)
1
  • 1
    Even better you can let(:verifying_double) { instance_double(YourClass) } and then ….and_return(verifying_double) and then expect(verifying_double).to… – this way it's clearer without nesting.
    – medik
    Commented Mar 29, 2021 at 15:02
4

It's sometimes error prone (I'll intermittently get an error saying "wrong number of arguments (0 for 1+)"; although this seems to only happen when performing multiple receive_message_chains in a single test), but you can also opt for chaining your "with" methods thus:

expect(User).to receive_message_chain(:where, :order).with(active: true).with('name ASC')
2
  • 1
    Nope. This will execute and pass, but it does not actually test the with parameter. (as of rspec 3.10, at least) Change :where, :order or name ASC to anything else, and you'll see your test still (misleadingly) pass. Commented Apr 16, 2021 at 21:05
  • @DavidHempy you are incorrect. The following passes: expect(NameserverPair).to receive_message_chain(:order, :first).with('domains_count ASC').with(no_args) The following does not: expect(NameserverPair).to receive_message_chain(:order, :first).with('domains_count ASC').with('bork') It correctly fails with: Failure/Error: order('domains_count ASC').first #<Double (anonymous)> received :first with unexpected arguments expected: ("bork") got: (no args) This is precisely the correct behaviour, using RSpec 3.7 Commented Apr 27, 2021 at 1:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.