Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails VCR gem-我可以将响应数据存储在单独的JSON文件中吗?_Ruby On Rails_Ruby_Testing_Rspec_Vcr - Fatal编程技术网

Ruby on rails VCR gem-我可以将响应数据存储在单独的JSON文件中吗?

Ruby on rails VCR gem-我可以将响应数据存储在单独的JSON文件中吗?,ruby-on-rails,ruby,testing,rspec,vcr,Ruby On Rails,Ruby,Testing,Rspec,Vcr,使用VCR gem,响应被保存为YAML盒式磁带文件中的一个大字符串。像这样: response: body: string: '{"data":{"salesforceObjects":{"records":[{"student":{"accountId" ... 但是,是否可以将此JSON保存在格式正确且易于阅读的单独文件中?来自官方: VCR。使用磁带('exampl

使用VCR gem,响应被保存为YAML盒式磁带文件中的一个大字符串。像这样:

 response:
    body:
      string: '{"data":{"salesforceObjects":{"records":[{"student":{"accountId" ...
但是,是否可以将此JSON保存在格式正确且易于阅读的单独文件中?

来自官方:

VCR。使用磁带('example',:serialize_with=>:json)do
将响应_body_放入(:get,“http://localhost:7777/foo,nil,'接受编码'=>'标识')
将响应_body_放入(:get,“http://localhost:7777/bar,nil,'接受编码'=>'标识')
结束

如果您编写了一个自定义盒式磁带持久器,如本文所述,该怎么办

您可以读取响应正文并将其存储在自定义文件中。然后,在读取时,将存储的响应主体添加到磁带中。 如果您只是想要一个格式非常好的响应副本供参考,甚至可能不需要这样做

(未经测试)


此代码段的哪一部分将JSON打印到文件中?我将seralize_与::json一起使用,但这只是将整个磁带转换为json而不是yaml-响应仍然是一个难看的字符串
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