Decrypter
Decrypter

Reputation: 3000

Mocking chain of methods in rspec

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)

Upvotes: 26

Views: 30471

Answers (1)

Decrypter
Decrypter

Reputation: 3000

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)

Upvotes: 44

Related Questions