Describe the bug
Can't seem to override config in testing my rails app. I have webmock set-up to mock external HTTP calls and I did need to mock the configcat request to get my tests running initially.
To reproduce
As per suggestion in docs, I created a Singleton via an initializer:
# config/initializers/configcat.rb
require 'configcat'
require 'singleton'
class FeatureFlagConfig
include Singleton
def initialize
@client = ConfigCat.create_client(ENV["CONFIGCAT_KEY"])
end
def get_value(flag)
@client.get_value(flag, false)
end
def refresh
@client.force_refresh
end
end
i create a stub as per webmock
require 'webmock'
include WebMock::API
def stub_configcat_config
stub_request(
:get,
"https://cdn-global.configcat.com/configuration-files/#{ENV['CONFIGCAT_KEY']}/config_v5.json"
).to_return(
status: 200,
body: {
"f": {
"my_feature": {
"v": false,
}
}
}.to_json
)
end
within my rails_helper.rb
, I add a before hook
config.before(:each, type: :request) do
WebMock.disable_net_connect!(allow: '127.0.0.1')
stub_configcat_config
FeatureFlagConfig.instance.refresh
puts FeatureFlagConfig.instance.get_value('my_feature')
end
If i now run 15 or 20 specs, while it's running, toggle the feature flag. It will show the returned value change from true
to false
(or V.V.) during the run.
Expected behavior
I expected the value I provided in my http mock_request to pverride the result. I guess somehow the caching is bypassing webmock. I anticipate this would cause problems in CI. I also want to avoid using a different Client / caching strategy for the test environment
Screenshots
bundle exec rspec spec/requests/
{"my_feature"=>false}
.{"my_feature"=>false}
.{"my_feature"=>false}
.{"my_feature"=>false}
.{"my_feature"=>false}
.{"my_feature"=>false}
.{"my_feature"=>false}
.*{"my_feature"=>true}
F{"my_feature"=>true}
.{"my_feature"=>true}
.{"my_feature"=>true}
F{"my_feature"=>true}
.**{"my_feature"=>true}
F{"my_feature"=>true}
.{"my_feature"=>true}