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)
(Or a mock in general, because this isn’t a concept unique to RSpec.) A mock is an object used for testing. You use mocks to test the interaction between two objects. Instead of testing the output value, like in a regular expectation. You’re writing an API that flips images.
Creating a double with RSpec is easy: A new double resembles a plain Ruby Object — it’s not very useful on its own. It is usually the first step before defining some fake methods on it. This is called method stubbing, and with RSpec 3 it is done using the allow () and receive () methods:
A new double resembles a plain Ruby Object — it’s not very useful on its own. It is usually the first step before defining some fake methods on it. This is called method stubbing, and with RSpec 3 it is done using the allow () and receive () methods:
If the method under test returns a value & it has no side effects (creating files, making API requests, etc.) then you don’t need a mock. Just check for the return value. If the method is working with external objects & sending orders to them, then you can mock the interactions with these objects.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With