This might sound like a terribly basic question... so let me explain the context. I'm retrieving an event from Stripe, but for the purposes of this, but this works for other things that I've considered.
The data that I need in the Stripe object is way buried, so I'd have to do event.data.object.date.lines.first.id
. I'm not sure how to mock/stub this...
The only way I can think to do this is create a fake model that simulates a Stripe event object (in this example for me, keep in mind that Stripe events can't be user created). And then I can mock that fake model. But is that really the way to do it? Is there some way I can just use Rspec to stub the whole shebang and say that event.data.object.date.lines.first.id
wherever it appears should just retun 1
?
Oh and also... if not, and it turns out I do have to create a fake model... tips greatly appreciated, because right now I'm thinking this "Fake" model needs many layers of children, and is totally not worth the time investment.
RSpec provides no special mechanisms to access elements under test, so yes, you would need to somehow
stub
theid
method and have it return whatever you wish (e.g.1
).To do that, you must have a way to access the
event
object in your test so that you can stub it'sdata
method. Once you can access to it, then you can use RSpec'sreceive_message_chain
matcher to specify the sequence of methods for which, if called, you will return1
.If there is some public
Strip
method you can mock to return adouble
for event, that would be the most straightforward approach.If that's not possible, you could determine the class of
event
and use theallow_any_instance_of
method to stub it'sdata
method. The resulting mock would be as follows, assumingFoo
was the class of theevent
object you're concerned about:Here's a test that passes: