【问题标题】:`Zlib::GzipFile::Error` when using `vcr` with rspec将 `vcr` 与 rspec 一起使用时出现`Zlib::GzipFile::Error`
【发布时间】:2013-11-30 07:03:09
【问题描述】:

我在做Railscast #291 Testing with VCR (Pro)

我想将 rspec 与 vcr 一起使用。没有 vcr 的测试通过此代码通过。

# spec/requests/zip_code_lookup_spec.rb
require "spec_helper"

describe "ZipCodeLookup" do
  it "show Beverly Hills given 90210" do
      visit root_path
      fill_in "zip_code", with: "90210"
      click_on "Lookup"
      page.should have_content("Beverly Hills")
  end
end

作为教程,我将代码放在VCR.use_cassette 中,如下所示:

require "spec_helper"

describe "ZipCodeLookup" do
  it "show Beverly Hills given 90210" do
    VCR.use_cassette "zip_code/90210" do
      visit root_path
      fill_in "zip_code", with: "90210"
      click_on "Lookup"
      page.should have_content("Beverly Hills")
    end
  end
end

并创建了这个文件:

# spec/support/vcr.rb
VCR.configure do |c|
  c.cassette_library_dir = Rails.root.join("spec", "vcr")
  c.stub_with :fakeweb
end

根据教程,rspec 测试应该会通过,但会失败并出现以下错误:

  1) ZipCodeLookup show Beverly Hills given 90210
     Failure/Error: click_on "Lookup"
     Zlib::GzipFile::Error:
       not in gzip format
     # ./app/models/zip_code.rb:6:in `initialize'
     # ./app/controllers/zip_code_lookup_controller.rb:3:in `new'
     # ./app/controllers/zip_code_lookup_controller.rb:3:in `index'
     # ./spec/requests/zip_code_lookup_spec.rb:8:in `block (3 levels) in <top (required)>'
     # ./spec/requests/zip_code_lookup_spec.rb:5:in `block (2 levels) in <top (required)>'

我不知道为什么gzip 会出现在这里,而不是出现在 Rails 项目的其余部分中。

我该如何解决这个问题?

【问题讨论】:

    标签: ruby-on-rails rspec


    【解决方案1】:

    刚刚遇到同样的问题:fakeweb gem 现在是 deprecated in VCR,请改用 webmock

    在您的 Gemfile 中,替换

    gem 'fakeweb'
    

    通过

    gem 'webmock'
    

    重新捆绑,你应该被排序

    【讨论】:

    • 感谢工作!我可以删除错误。但是出现了另一个错误Failure/Error: click_on "Lookup" NoMethodError:undefined method []' for nil:NilClass`。我正在寻找解决方案,但直到现在还没有找到。如果你不知道这个错误,我会再问一个问题。
    • 我在config/environments/test.rb 中添加了WebMock.allow_net_connect!(:net_http_connect_on_start =&gt; true),然后它就可以正常工作了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-20
    • 2012-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多