Tom Rossi
Tom Rossi

Reputation: 12086

Disable HTTP requests unless they go through WebMock

I'm trying to make sure that all HTTP requests go through WebMock. I would expect this code to fail with some sort of WebMock exception, but it executes just fine:

require "test_helper"
require "webmock/minitest"

class URLTest < ActiveSupport::TestCase
  test "valid urls" do
    WebMock.enable!
    WebMock.disable_net_connect!
    Net::HTTP.get("www.google.com", "/") # Should raise WebMock::NetConnectNotAllowedError
  end
end

How can I ensure that all HTTP requests are going through WebMock?

I am using Rails 7.2, Ruby 3.22, and VCR 6.2.0.

Here is my VCR config:

  VCR.configure do |c|
    c.cassette_library_dir = Rails.root.to_s + "/test/vcr_cassettes"
    c.hook_into :webmock
    c.allow_http_connections_when_no_cassette = true
    c.default_cassette_options= {:serialize_with => :yaml, :record => :once, :allow_playback_repeats => true}
  end

Upvotes: 1

Views: 127

Answers (0)

Related Questions