3

I am trying to stub a call that is made when validating a connection made based on settings during the create process on my model.

Disabling the callbacks seems messy as it means maintaining several instances of the callback (likely to change in future) and VCR seems like the more elegant solution. I have it so far loading the cassette correctly and can see it when calling VCR.cassettes whilst in byebug but the request is producing the error *** Net::OpenTimeout Exception: execution expired.

I assume this is because of something I'm missing or it's just not possible?

Example of setup:

model.rb

class ConnectionClass
  ...
  after_commit :validate_via_api on: :create

  ...

  def validate_via_api
    byebug # calling VCR.cassettes here shows the loaded cassette set up in the factory
    go_do_api_call_that_should_be_stubbed # this goes and makes the call that times out
  end
end

factory.rb

FactoryBot.define do
  ...
  factory :connection_factory, class: ConnectionClass do
    ...
    before(:create) do |connection|
      VCR.insert_cassette("the_cassette", erb: { name: connection.name, url: connection.url })
    end

    after(:create) do
      VCR.eject_cassette
    end
  end
end

Varsions: vcr (3.0.3) rails (= 3.2.22.5) ruby-2.3.3 rspec (3.6.0) factory_bot (4.11.1)

EDIT: I have also now found this to be the case in let() as well when using VCR.use_cassette...

RKBuiltMyRothod
  • 101
  • 1
  • 8
  • > EDIT: I have also now found this to be the case in let() as well when using VCR.use_cassette... can you elaborate on that? i have the same issue and the only workaround i found so far is: `let(:my_model) { VCR.use_cassette("my-cassettee") { create(:my_model) } }` – Kamil Gwóźdź Feb 13 '22 at 13:15
  • @KamilGwóźdź Hmm, when I try that at the head of my rspec I dont seem to hit the VCR cassette. I tried this by loading a new cassette `let(:my_model) { VCR.use_cassette("new_cassette_name") { create(:my_model) } }` but no new cassette was created. – RKBuiltMyRothod Feb 14 '22 at 14:32
  • What's your vcr version? – Kamil Gwóźdź Feb 14 '22 at 19:14
  • @KamilGwóźdź `vcr (3.0.3)` I'll update the question with Rails/Ruby versions as well in case that might bring some clues – RKBuiltMyRothod Feb 16 '22 at 10:19
  • 1
    I'm on vcr = 5.1.0, ruby = 2.7.2 and I used `record: :once`. – Kamil Gwóźdź Feb 16 '22 at 11:30

0 Answers0