Reputation: 7419
Using the VCR gem, responses are saved as a large string inside the YAML cassette file. Like this:
response:
body:
string: '{"data":{"salesforceObjects":{"records":[{"student":{"accountId" ...
However, is it possible to save this JSON in a separate file, which is properly formatted and easier to read?
Upvotes: 1
Views: 1242
Reputation: 1585
What if you wrote a custom cassette persister, like documented in here?
https://relishapp.com/vcr/vcr/v/2-9-1/docs/cassettes/cassette-persistence
You can read the response body and store it in a custom file. Then, upon reading, you add the response body stored to the cassette. This might not even be required if you just want a pretty-formatted copy of the response for reference.
Something on the lines of: (not tested)
class PrettyCassetteBodyPersister
# dunno if content is a string or hash. Might be missing some serialization / deserialization
# might require extra logic to make it work with multiple request cassettes
def [](name)
content = YAML.load IO.binread("cassettes/#{name}")
response_body = JSON.parse IO.binread("cassette_bodies/#{name}")
content['response']['body'] = response_body
content
end
def []=(name, content)
IO.binwrite("cassettes/#{name}", content)
IO.binwrite("cassette_bodies/#{name}", content['response']['body']
end
end
VCR.configure do |c|
c.cassette_persisters[:copy_bodies] = PrettyCassetteBodyPersister.new
c.default_cassette_options = { :persist_with => :copy_bodies }
end
Upvotes: 1
Reputation: 4440
From official docs:
VCR.use_cassette('example', :serialize_with => :json) do
puts response_body_for(:get, "http://localhost:7777/foo", nil, 'Accept-Encoding' => 'identity')
puts response_body_for(:get, "http://localhost:7777/bar", nil, 'Accept-Encoding' => 'identity')
end
Upvotes: 1