Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TestHelp: Fix setup for RSpec, clear adapter before each test #827

Merged
merged 2 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions lib/flipper/test_help.rb
Original file line number Diff line number Diff line change
@@ -1,37 +1,42 @@
module Flipper
module TestHelp
extend self

def flipper_configure
# Create a single shared memory adapter instance for each test
@flipper_adapter = Flipper::Adapters::Memory.new
# Use a shared Memory adapter for all tests. This is instantiated outside of the
# `configure` block so the same instance is returned in new threads.
adapter = Flipper::Adapters::Memory.new

Flipper.configure do |config|
config.adapter { @flipper_adapter }
config.adapter { adapter }
config.default { Flipper.new(config.adapter) }
end
end

def flipper_reset
Flipper.instance = nil # Reset previous flipper instance
# Remove all features
Flipper.features.each(&:remove) rescue nil

# Reset previous DSL instance
Flipper.instance = nil
end
end
end

if defined?(RSpec) && RSpec.respond_to?(:configure)
RSpec.configure do |config|
config.include Flipper::TestHelp
config.before(:each) do
flipper_reset
flipper_configure
end
config.before(:suite) { Flipper::TestHelp.flipper_configure }
config.before(:each) { flipper_reset }
end
end

if defined?(ActiveSupport)
ActiveSupport.on_load(:active_support_test_case) do
Flipper::TestHelp.flipper_configure

ActiveSupport::TestCase.class_eval do
include Flipper::TestHelp

setup :flipper_configure
setup :flipper_reset
end
end
Expand Down
9 changes: 7 additions & 2 deletions test_rails/system/test_help_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,13 @@ class TestHelpTest < ActionDispatch::SystemTestCase
# Any driver that runs the app in a separate thread will test what we want here.
driven_by :cuprite, options: { process_timeout: 30 }

# Ensure this test uses this app instance
setup { Rails.application = TestApp.instance }
setup do
# Reconfigure Flipper since other tests change the adapter.
flipper_configure

# Ensure this test uses this app instance
Rails.application = TestApp.instance
end

test "configures a shared adapter between tests and app" do
Flipper.disable(:test)
Expand Down