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
stubtheidmethod and have it return whatever you wish (e.g.1).To do that, you must have a way to access the
eventobject in your test so that you can stub it'sdatamethod. Once you can access to it, then you can use RSpec'sreceive_message_chainmatcher to specify the sequence of methods for which, if called, you will return1.If there is some public
Stripmethod you can mock to return adoublefor event, that would be the most straightforward approach.If that's not possible, you could determine the class of
eventand use theallow_any_instance_ofmethod to stub it'sdatamethod. The resulting mock would be as follows, assumingFoowas the class of theeventobject you're concerned about:Here's a test that passes: