如何编辑VCR gem返回的响应体?

Cle*_*ton 11 ruby-on-rails vcr

我正在使用录像机记录我正在整合的其他系统的响应.

但是,这个响应获得了一个巨大的JSON,VCR正在以二进制格式保存它:

body:
  encoding: ASCII-8BIT
  string: !binary |-
    eyJsaXN0IjpbXSwiZmFjZXRzIjpbeyJuYW1lIjoiU2FsZXNDaGFubmVsTmFt
    ZSIsInR5cGUiOi...
Run Code Online (Sandbox Code Playgroud)

有没有办法我只能将响应体保存为JSON?

我想这样做来编辑返回的JSON,以便为我的测试制作其他场景,

谢谢

pol*_*tti 23

从Cleyton提供的谷歌论坛链接(如果我有代表,我会发表评论),以下更改spec_helper.rb为我工作:

VCR.configure do |c|
  c.before_record do |i|
    i.response.body.force_encoding('UTF-8')
  end
end
Run Code Online (Sandbox Code Playgroud)


Tlm*_*aK0 5

decode_compressed_response在您的配置中使用。

VCR.configure do |c|
  c.cassette_library_dir = 'cassettes'
  c.hook_into :webmock
  c.default_cassette_options = { :decode_compressed_response => true } 
end
Run Code Online (Sandbox Code Playgroud)


Cle*_*ton 4

在 VCR 谷歌论坛中查看,我被告知要做我自己的序列化器以获得漂亮的 json 返回。

所以,我找到了这段代码。通过一些小的修改,它解决了我的问题,将响应正文格式化为不编码为二进制。