0

I'm attempting to use VCR 2.0.0 outside of Rails.

VCR seems to create the vcr_cassettes directory perfectly when I run the specs. However, the tests seem to be still hitting the network and there are never any yaml cassettes to be found in the cassette directory.

Any ideas what I'm doing wrong?

Here is an example of me running the specs in question:

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

I have a spec/support/vcr_helper.rb:

require "vcr"

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

And I have a spec file under 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

And just incase you're interested, here's the method being tested in 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
Myron Marston
  • 21,452
  • 5
  • 64
  • 63
David Tuite
  • 22,258
  • 25
  • 106
  • 176

1 Answers1

1

I believe your problem is that VCR only records and plays back HTTP connections, while the Whois gem uses TCPSocket and not any of the http libraries VCR knows how to proxy.

dbenhur
  • 20,008
  • 4
  • 48
  • 45
  • 1
    You could probably just place a simple expectation on NetChecker's Whois collaborator to isolate the test from the network, eg: `Whois.expects(:available?).with('apple.com').returns(false)` – dbenhur Mar 18 '12 at 23:32
  • Yeah looks like I'm going to have to do that. It's just that I like to reach a little further out when I'm testing around the edges of my system. – David Tuite Mar 18 '12 at 23:47