Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to edit response body returned by VCR gem?

I'm using VCR to record responses from other systems that i'm integrating with.

But, this response get a huge JSON an VCR is saving it in a binary format:

body:
  encoding: ASCII-8BIT
  string: !binary |-
    eyJsaXN0IjpbXSwiZmFjZXRzIjpbeyJuYW1lIjoiU2FsZXNDaGFubmVsTmFt
    ZSIsInR5cGUiOi...

Is there a way where I can save ONLY the response body as JSON?

I want to do this to edit the returned JSON in order to make other scenarios for my tests,

Thanks

like image 498
Cleyton Avatar asked Feb 20 '14 21:02

Cleyton


People also ask

What is VCR test?

VCR is a tool which I'd classify as useful in snapshot testing. You record a snapshot of an interaction with a System Under Test. Once recorded, these interactions are replayed from stored files — snapshots. VCR specifically records HTTP interactions and stores results of such in YAML files called “tapes”.

What is VCR in rails?

Testing external APIs can be a time-consuming task, VCR is a ruby gem that allows you to record test suite's HTTP interactions and replay them during future test runs for fast, deterministic, accurate tests.


1 Answers

From the google forum link that Cleyton supplied (I would leave a comment if I had the rep), the following change in spec_helper.rb works for me:

VCR.configure do |c|
  c.before_record do |i|
    i.response.body.force_encoding('UTF-8')
  end
end
like image 190
polpetti Avatar answered Sep 29 '22 16:09

polpetti