【问题标题】:Unable to record Whois requests with VCR?无法使用 VCR 记录 Whois 请求?
【发布时间】:2012-03-18 20:46:05
【问题描述】:

我正在尝试在 Rails 之外使用 VCR 2.0.0。

当我运行规范时,VCR 似乎完美地创建了vcr_cassettes 目录。但是,测试似乎仍在网络上,并且在盒式磁带目录中从未找到任何 yaml 盒式磁带。

任何想法我做错了什么?

这是我运行相关规范的示例:

rm -r spec/fixtures/vcr_cassettes

rspec spec/lib/availability_checkers/net_checker_spec.rb
...*
Finished in 1.83 seconds
3 examples, 0 failures, 0 pending

ls spec/fixtures/vcr_cassettes
=> # created but empty

我有一个spec/support/vcr_helper.rb

require "vcr"

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

我在spec/lib/availability_checkers/net_checker_spec.rb 下有一个规范文件:

require "availability_checkers/net_checker'
require "support/vcr_helper"

describe NetChecker, "check" do
  it "should return false if the domain is unavailable" do
    VCR.use_cassette("unavailable_domain") do
      NetChecker.check("apple.com").should be_false
    end
  end
end

如果你有兴趣,这里是 lib/availability_checkers/net_checker.rb 中正在测试的方法:

require "whois"

class NetChecker
  def check(host)
    # this part hits the network
    return Whois.available?(host)
  rescue Whois::Error => e
    return nil
  end
end

【问题讨论】:

    标签: ruby rspec rubygems rspec2 vcr


    【解决方案1】:

    我相信您的问题是 VCR only records and plays back HTTP connections,而 Whois gem uses TCPSocket 而不是任何 http 库 VCR 都知道如何代理。

    【讨论】:

    • 您可能只希望 NetChecker 的 Whois 合作者将测试与网络隔离,例如:Whois.expects(:available?).with('apple.com').returns(false)
    • 是的,看来我必须这样做。只是当我在我的系统边缘进行测试时,我喜欢走得更远一点。
    猜你喜欢
    • 1970-01-01
    • 2023-03-09
    • 2023-04-07
    • 2011-01-14
    • 2021-04-10
    • 1970-01-01
    • 1970-01-01
    • 2020-10-07
    • 2017-10-13
    相关资源
    最近更新 更多