I'm using VCR with RSpec to unit test a section of API code, and I've hit a small challenge.
My client code uses sleep
to rate limit API requests. Thanks to this elegant solution, I can now intercept the sleep
message with a stub from my unit tests:
expect(subject).to receive(:sleep)
The problem is that while I'm developing this code, I've got to delete my cassettes frequently, but since I've stubbed sleep
, there's no rate limit on the real API requests and the spec blows up.
What [I think] I want to do is something like the following:
expect(subject).to receive(:sleep) if !vcr.real_request?
So that I don't interfere with the subjects sleep
when I'm recording a cassette.
I see interesting related discussion, but it seems like a different case and I don't see how I can leverage it directly. However, that did lead me to discover VCR hooks (esp. before_playback
) but it's not clear to me how I would export subject
into the scope of the block, or else proxy block execution into the scope of my test to make the stub.
Any thoughts?
NB: I'm using the config.configure_rspec_metadata!
feature in my spec_helper.rb
to automatically capture/name my cassettes, if that makes any difference (i.e. I'm not wrapping each individual API method explicitly in its own VCR block).