Rspec + savon + vcr没有录音

 速度向前迈进 发布于 2023-02-09 13:10

我无法用录像机录制任何内容,我有这样的设置:

spec_helper.rb

require 'vcr'
VCR.configure do |c|
  c.cassette_library_dir = 'spec/cassettes'
  c.hook_into :webmock
  c.configure_rspec_metadata!
  c.default_cassette_options = { :record => :new_episodes }
end

并测试:

describe SomeClass do
  describe '#foo', vcr: true do
    it 'should do http request', do
      expect(subject.do_request).to be_true
    end
  end
end

运行该规范会导致:

.HTTPI executes HTTP GET using the net_http adapter
SOAP request: (...)

完全像没有录像机.遗憾的是文档中没有任何内容.我在这里报告了类似的问题,但要求httpi不会产生任何影响.要做到这一点应该怎么做?

1 个回答
  • 让我让VCR记录正确匹配的SOAP请求,我指定了更多的匹配器供它使用.由于使用SOAP,url端点通常是相同的,但每个请求的主体内容/标头是不同的.注意,:method,:uri,:headers.你也可以在身体上匹配,但这对我的用例来说足够了,因为我们的标题非常详细.我正在使用这个:

    VCR.configure do |c|
      c.hook_into :webmock # fakeweb fails with savon
      c.ignore_hosts 'www.some-url.com'
      c.configure_rspec_metadata!
      c.ignore_localhost                        = true
      c.cassette_library_dir                    = 'spec/support/vcr_cassettes'
      c.allow_http_connections_when_no_cassette = true
      c.default_cassette_options                = { allow_playback_repeats: true, match_requests_on: [:method, :uri, :headers] }
      # c.debug_logger                            = File.open(Rails.root.join('log/vcr.log'), 'w')
    end
    

    然后我用空哈希标记规范,以防我想覆盖全局VCR配置,如:

    describe SomeClass do
      describe '#foo', vcr: {} do
        # test something
      end
    end
    

    最后,您可以让VCR/Webmock更多的请求忽略了更容易调试.因此,如果您有许多JS调用或类似,请将这些请求添加到'ignore_hosts'选项中.我在全局配置中的最后一行显示了如何让VCR记录它正在做的事情,也很有帮助.

    2023-02-09 13:11 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有