Skip to content

Commit

Permalink
Merge pull request #827 from flippercloud/test-help-fixes
Browse files Browse the repository at this point in the history
TestHelp: Fix setup for RSpec, clear adapter before each test
  • Loading branch information
bkeepers authored Jan 19, 2024
2 parents 6384a80 + bcafbc4 commit e947540
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
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

0 comments on commit e947540

Please sign in to comment.