Serge Vinogradoff
Serge Vinogradoff

Reputation: 2272

undefined method `receive_message_chain'

Surprisingy can't find anything about this.

Trying to stub this chain of methods:

Twitter::REST::Client.new(<some credentials>).user.followers_count

What I actually want to do is this:

allow_any_instance_of(Twitter::REST::Client).to receive_message_chain(:user, :followers_count).and_return 0

But it did not work. Gave an error undefined method 'receive_message_chain'.

So I found this tutorial on Rspec-Mocks 3.0: https://github.com/rspec/rspec-mocks/blob/master/features/method_stubs/receive_message_chain.feature

And tried this:

subject = Twitter::REST::Client
allow(subject).to receive_message_chain(:user, :followers_count).and_return 0
expect(Twitter::REST::Client.new.users.followers_count).to eq 0

Just to try it out - and it did not work either. Is there anything I'm doing wrong?

I do have rspec-mocks (3.0.0.beta1) in my Gemfile.lock.

Other types of stubs (like .to receive(:method) work fine.

Upvotes: 2

Views: 1300

Answers (1)

Peter Alfvin
Peter Alfvin

Reputation: 29389

The method receive_message_chain was introduced in 3.0.0.beta2, as described in https://github.com/rspec/rspec-mocks/blob/master/Changelog.md#300beta2--2014-02-17, so if you upgrade your version of rspec-mocks (currently at 3.0.0.rc1), you should be ok with that syntax.

Upvotes: 5

Related Questions