Module: Toggly::Rails::Testing

Included in:
MinitestHelpers, RSpecHelpers
Defined in:
lib/toggly/rails/testing.rb

Overview

Testing helpers for RSpec and Minitest.

Examples:

RSpec

RSpec.configure do |config|
  config.include Toggly::Rails::Testing::RSpecHelpers
end

describe "my feature" do
  it "shows new UI when enabled" do
    with_feature(:new_ui, enabled: true) do
      visit dashboard_path
      expect(page).to have_content("New Dashboard")
    end
  end
end

Minitest

class MyTest < ActionDispatch::IntegrationTest
  include Toggly::Rails::Testing::MinitestHelpers

  test "shows new UI when enabled" do
    with_feature(:new_ui, enabled: true) do
      get dashboard_path
      assert_includes response.body, "New Dashboard"
    end
  end
end

Defined Under Namespace

Modules: MinitestHelpers, RSpecHelpers

Instance Method Summary collapse

Instance Method Details

#disable_feature(feature_key) { ... } ⇒ Object

Disable a feature for the duration of a block

Parameters:

  • feature_key (String, Symbol)

    Feature key

Yields:

  • Block during which the feature is disabled



93
94
95
# File 'lib/toggly/rails/testing.rb', line 93

def disable_feature(feature_key, &block)
  with_feature(feature_key, enabled: false, &block)
end

#enable_feature(feature_key) { ... } ⇒ Object

Enable a feature for the duration of a block

Parameters:

  • feature_key (String, Symbol)

    Feature key

Yields:

  • Block during which the feature is enabled



85
86
87
# File 'lib/toggly/rails/testing.rb', line 85

def enable_feature(feature_key, &block)
  with_feature(feature_key, enabled: true, &block)
end

#with_feature(feature_key, enabled: true) { ... } ⇒ Object

Stub a feature to return a specific value

Parameters:

  • feature_key (String, Symbol)

    Feature key

  • enabled (Boolean) (defaults to: true)

    Whether the feature should be enabled

Yields:

  • Block during which the feature is stubbed



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/toggly/rails/testing.rb', line 38

def with_feature(feature_key, enabled: true)
  key = feature_key.to_s
  original_definitions = Toggly.client&.definitions&.dup || {}

  # Add or update the feature definition
  Toggly.client.instance_variable_get(:@mutex).synchronize do
    Toggly.client.definitions[key] = Toggly::FeatureDefinition.new(
      feature_key: key,
      enabled: enabled
    )
  end

  yield
ensure
  # Restore original definitions
  Toggly.client&.instance_variable_get(:@mutex)&.synchronize do
    Toggly.client.instance_variable_set(:@definitions, original_definitions)
  end
end

#with_features(features) { ... } ⇒ Object

Stub multiple features at once

Parameters:

  • features (Hash<String, Boolean>)

    Hash of feature keys to enabled states

Yields:

  • Block during which the features are stubbed



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/toggly/rails/testing.rb', line 62

def with_features(features)
  original_definitions = Toggly.client&.definitions&.dup || {}

  features.each do |key, enabled|
    Toggly.client.instance_variable_get(:@mutex).synchronize do
      Toggly.client.definitions[key.to_s] = Toggly::FeatureDefinition.new(
        feature_key: key.to_s,
        enabled: enabled
      )
    end
  end

  yield
ensure
  Toggly.client&.instance_variable_get(:@mutex)&.synchronize do
    Toggly.client.instance_variable_set(:@definitions, original_definitions)
  end
end