Testing
The great thing about Flipper is that the storage of feature flags can be adapted to whatever. This means you can use different storage mechanisms for different environments.
Starting Fresh
For CI/test environments, we recommend the memory adapter. Switching the default instance of Flipper to use the memory adapter is easy. Before each test, simply configure Flipper to use the memory adapter like so:
# Rails/Minitest example
class ActiveSupport::TestCase
setup do
Flipper.instance = Flipper.new(Flipper::Adapters::Memory.new)
end
end
# RSpec example
RSpec.configure do |config|
config.before(:each) do
Flipper.instance = Flipper.new(Flipper::Adapters::Memory.new)
end
end
Including the snippet above means that each test will start fresh with all features disabled.
We find this the best route, but your application may have different needs. If you want some features to always be enabled, you can enable them right after switching to the memory adapter.
class ActiveSupport::TestCase
setup do
Flipper.instance = Flipper.new(Flipper::Adapters::Memory.new)
Flipper.enable :accounts
Flipper.enable :plausible
end
end
In the example above, the accounts
and plausible
features would both start enabled for every test. Every other feature flag would start disabled.
Test Both Sides
Though defaulting to enabled is popular in the Flipper community, we typically take a bit different approach. For any feature flag, we usually have at least one test for when the feature is enabled and disabled. This ensures both sides of the coin are tested.
require 'test_helper'
class DocumentationControllerTest < ActionDispatch::IntegrationTest
test "returns 404 for documentation when feature disabled" do
Flipper.disable(:docs)
get documentation_path
assert_response :not_found
end
test "renders documentation when feature enabled" do
Flipper.enable(:docs)
get documentation_path
assert_response :success
end
end
That should be enough to get you started on the path to testing around your feature flags. But if you have any more questions, don't hesitate to reach out to us.