Mocking chain of methods in rspec

Decrypter picture Decrypter · Nov 13, 2016 · Viewed 10.3k times · Source

There are a chain of methods which gets a user object. I am trying to mock the following to return a user in my Factory Girl

@current_user = AuthorizeApiRequest.call(request.headers).result

I can mock the object up until the call method but I'm stuck at mocking the result method

allow(AuthorizeApiRequest).to receive(:call).and_return(:user)

Answer

Decrypter picture Decrypter · Nov 13, 2016

I found I need to use receive_message_chain

So this worked for me.

allow(AuthorizeApiRequest).to receive_message_chain(:call, :result).and_return(user)