我无法用录像机录制任何内容,我有这样的设置:
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
不会产生任何影响.要做到这一点应该怎么做?
让我让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记录它正在做的事情,也很有帮助.