vcrpy
vcrpy copied to clipboard
Nested VCR context not working
trafficstars
Hi! I think I found a bug... or at least something that is not working as the VCR gem (which vcrpy is said to be inspired on).
In the Ruby version, you can have nested Cassettes:
VCR.use_cassette("outer") do
make_some_http_request
VCR.use_cassette("inner_1") do
make_some_other_http_request
VCR.use_cassette("inner_2") do
make_yet_another_request
end
end
end
Given the following code, one would assume the example.com request be played/recorded from all_dummy_get.yml file:
outer_vcr = VCR(cassette_library_dir='tests/testing/cassettes')
with outer_vcr.use_cassette('all_dummy_get.yml') as outer_cassette:
inner_vcr = VCR(cassette_library_dir='tests/testing/cassettes', ignore_hosts=['example.com'])
with inner_vcr.use_cassette('example_dummy_get.yml') as inner_casette:
requests.get('https://example.com')
assert inner_cassette.play_count == 0
assert outer_cassette.play_count == 1
However, running the above will fail on the second assert because all_dummy_get.yml file is never recorded to (so there's no cassette, meaning no playback). It seems the inner VCR context is overwriting the outer one and "resetting" all mocks to their "original original" (before messing with any patching) that VCR creates to do its magic.
Is there any way to make this work?