Ruby VCR testing is not capturing http calls from sinatra

18 views Asked by At

I've got a Sinatra app which has the following call in it;

  get '/test' do 
    @res = Net::HTTP.get_response(URI('http://www.iana.org/domains/reserved'))
    erb :test
  end

With view;

<%= @res.body %>

I'm testing this call using Capybara + Selenium + VCR and it's not creating a cassette. The test passes but because VCR isn't recording properly I don't get the speed benefit.

#!/usr/bin/env ruby

require 'test/unit'
require 'capybara'
require 'capybara/dsl'
require 'webdrivers/chromedriver'
require 'webmock'
require 'vcr'
require 'dotenv'
require_relative "../sinatra_app.rb"

class SinatraAppTest < Test::Unit::TestCase
  include Capybara::DSL
  
  def setup
    Dotenv.load "#{__dir__}/../.env"
    Capybara.register_driver :chrome do |app|
      Capybara::Selenium::Driver.new(app, :browser => :chrome, options: chrome_options)
    end
    Capybara.default_driver = :chrome
    Capybara.default_max_wait_time = 20
    Capybara.app_host = "http://localhost:9393"

    VCR.configure do |config|
      config.cassette_library_dir = "fixtures/vcr_cassettes2"
      config.hook_into :webmock
      config.allow_http_connections_when_no_cassette = true
      config.default_cassette_options = {re_record_interval: (3600 * (24 * 30))}
      config.ignore_localhost = true
    end
  end
  

  # Settings and profile for the Chrome Browser
  # NOTE: still cannot get headless working
  def chrome_options
    opts = Selenium::WebDriver::Chrome::Options.new
    # opts.add_argument('--headless') unless ENV['UI']
    opts.add_argument('--no-sandbox')
    opts.add_argument('--disable-gpu')
    opts.add_argument('--disable-dev-shm-usage')
    opts.add_argument('--window-size=1920,1080')
    opts
  end

  def test_iana
    VCR.use_cassette("iana") do
      visit "/test"
      assert page.has_content?("Example domains")
    end
  end

  def teardown
    Capybara.reset_sessions!
    Capybara.use_default_driver
  end
end

How do I capture the internal calls from Sinatra and record them through VCR?

If I place the Net::HTTP call within my test then I get the correct recording but that's not how I want to drive the test.

0

There are 0 answers