Avoid doing http calls in test env

Hi!

I’m using dry-system to define some shared http clients in my app. Indeed, some services need to do API calls to Azure, so I used dry-system in an initializer to define global “azure_clients” without having to log-in before every single azure-related stuff.

In my tests though, I don’t really want to try logging in Azure, so I defined this in my spec_helper :

RSpec.configure do |config|
  config.before :suite do
    MyApi::Container.enable_stubs!
    MyApi::Container.stub :azure_client, {}
  end
end

However, launching my app in test env doesn’t prevent doing the authentication stuff ; they are just overrided when I’m getting into my controllers.

I tried to simply add next if Rails.env.test? on top of my container, but then, stubbing the variables in my container doesn’t work anymore :

Failure/Error: MyApi::Container.stub :azure_client, {}

ArgumentError:
  cannot stub "azure_client" - no such key in container

What can I do ? It seems like I could do something like this in my container

  if Rails.env.test?
    register(:azure_client, {foo: 'bar'})
    register(:other_client, {bar: 'baz'})
    next
  end

but then, I don’t really get the point in stubbing it in my spec_helper. Moreover, having to do test-related stuff in the container directly doesn’t look good to me ; it obviously should be done in the spec_helper.

Did I miss something about the configuration of this gem & its test environment?

Thank you very much :slight_smile:

1 Like

Did you find a solution ?
I’m having a similar issue : Stubbing a Provider (formerly known as Bootable component) - #2 by systho

May be https (443) will help?