Possible to load VCR cassette inside before(:create) in factory_bot Factory?

524 views Asked by At

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...

0

There are 0 answers