带有Webmock的录像机错误地将Typhoeus打断(未定义的方法`> ='为nil:NilClass)

时间:2014-05-20 19:16:07

标签: ruby ruby-on-rails-4 vcr typhoeus webmock

我想要的是:每次测试运行时,外部API调用都会返回相同的内容 我得到的:第一次VCR保存它,Typhoeus第二次返回错误

我运行测试一次,它会产生实际请求(当我创建组织和用户时发生),然后当我再次运行它时,它无法正确解析请求。

首先运行:

1) AccountSettingsController GET #show responds successfully with an HTTP 200 status code
   Failure/Error: expect(response).to be_success
     expected success? to return true, got false
   # ./spec/controllers/account_settings_spec.rb:12:in `block (3 levels) in <top (required)>'

以下所有操作直到我删除录像带:

1) AccountSettingsController GET #show responds successfully with an HTTP 200 status code
   Failure/Error: organization = Fabricate(:organization)
   NoMethodError:
     undefined method `>=' for nil:NilClass
   # ./app/models/hand.rb:45:in `create'
   # ./app/models/hand.rb:64:in `save'
   # ./app/models/hand.rb:70:in `save!'
   # ./spec/controllers/account_settings_spec.rb:6:in `block (3 levels) in <top (required)>'

回溯到这些线条(来自我使用的私人宝石)

def create(values)
  response = Typhoeus.post(resource_url, {body: values.to_json}.merge(headers))

  process_as_json response.options[:response_body]
end

def process_as_json(response)
  if !response.empty?
    JSON.parse(response)
  else
    nil
  end
end

版本:

  • 录像机(2.9.0)
  • Webmock(1.18.9)
  • Typhoeus(0.6.8)

设定:

# spec/spec_helper.rb

require "simplecov"
SimpleCov.start "rails"

ENV["RAILS_ENV"] ||= "test"
require File.expand_path("../../config/environment", __FILE__)

require "rspec/rails"
require "rspec/autorun"
require "capybara/rspec"
require "webmock/rspec"

Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }

Fabrication.configure do |config|
  config.path_prefix = Rails.root
end

VCR.configure do |config|
  config.cassette_library_dir = "spec/vcr_cassettes"
  config.hook_into :webmock
  config.configure_rspec_metadata!
end

RSpec.configure do |config|
  config.order = "random"
  config.treat_symbols_as_metadata_keys_with_true_values = true
end

测试:

# spec/controllers/account_settings_spec.rb

require "spec_helper"

describe AccountSettingsController do
  describe "GET #show" do
    it "responds successfully with an HTTP 200 status code", :vcr do
      organization = Fabricate(:organization)
      user = Fabricate(:user, organization_id: organization.id)

      get :show

      expect(response).to be_success
      expect(response.status).to eq(200)
    end
  end
end

1 个答案:

答案 0 :(得分:1)

根据您提供的详细信息,无法明确回答您的问题,但这里有一些建议:

  • 使用debug_logger option获取VCR所做的详细输出;这可以给你一些关于发生了什么的提示。
  • 尝试使用hook_into :typhoeus而不是hook_into :webmock。当您直接挂入台风时,录像机能够更准确地录制和播放,而不是使用webmock作为中间层。