VCR不会在成功请求时录制磁带,仅在失败的请求上录制磁带

时间:2014-10-10 22:08:59

标签: facebook-graph-api rspec vcr

我有一个简单的测试来获取一个Facebook对象。我正在使用Curl来处理请求。

it "gets an object from Facebook" do
  VCR.use_cassette('facebook') do
    url = "https://graph.facebook.com/<ID>?access_token=#{@access_token}&#{query_string}"
    curl = Curl::Easy.perform(url)
    expect(curl.body_str).to eql('<my object>')
  end
end

我的VCR配置是:

VCR.configure do |c|
  c.cassette_library_dir = 'spec/fixtures/vcr_cassettes'
  c.hook_into :webmock
end

当我运行测试时,它会通过,并记录以下内容:

[Cassette: 'facebook'] Initialized with options: {:record=>:once, :match_requests_on=>[:method, :uri], :allow_unused_http_interactions=>true, :serialize_with=>:yaml, :persist_with=>:file_system}
[webmock] Handling request: [get https://graph.facebook.com/<ID>?access_token=<TOKEN>&fields=%5B%22id%22,%22account_id%22,%22name%22,%22campaign_group_status%22,%22objective%22%5D] (disabled: false)
[Cassette: 'facebook'] Initialized HTTPInteractionList with request matchers [:method, :uri] and 0 interaction(s): {  }
[webmock] Identified request type (recordable) for [get https://graph.facebook.com/<ID>?access_token=<TOKEN>&fields=%5B%22id%22,%22account_id%22,%22name%22,%22campaign_group_status%22,%22objective%22%5D]

但录音带没有录音,目录是空的。我已尝试:record => :all同样的结果。

通常,当人们使用不兼容的钩子时,人们会遇到这个错误,但事实并非如此。我正在使用webmock和curb。

好奇地,当请求失败时记录盒式磁带,例如令牌过期。当它被修复后,我删除了文件,它不再被记录。

有没有人有同样的问题?

1 个答案:

答案 0 :(得分:1)

事实证明,我的代码比上面的代码稍微复杂一些,并且在执行请求后执行回调。类似的东西:

success_handler = Proc.new { return c.body_str }

curl.on_success do |easy|
  success_handler.call(easy)
end

绕过VCR并且不写入文件。重构代码以不使用回调。