Avoid doing http calls in test env


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.stub :azure_client, {}

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, {}

  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'})

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: