Module: Gitlab::Experiment::RSpecHelpers

Defined in:
lib/gitlab/experiment/rspec.rb

Instance Method Summary collapse

Instance Method Details

#stub_experiments(experiments) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/gitlab/experiment/rspec.rb', line 36

def stub_experiments(experiments)
  experiments.each do |experiment|
    wrapped_experiment(experiment, remock: true) do |instance, wrapped|
      # Stub internal methods that will make it behave as we've instructed.
      allow(instance).to receive(:enabled?) { wrapped.variant_name != false }

      # Stub the variant resolution logic to handle true/false, and named variants.
      allow(instance).to receive(:resolve_variant_name).and_wrap_original { |method|
        # Call the original method if we specified simply `true`.
        wrapped.variant_name == true ? method.call : wrapped.variant_name
      }
    end
  end

  wrapped_experiments
end

#wrapped_experiment(experiment, remock: false, &block) ⇒ Object



53
54
55
56
57
58
59
60
61
62
# File 'lib/gitlab/experiment/rspec.rb', line 53

def wrapped_experiment(experiment, remock: false, &block)
  klass, experiment_name, variant_name = *extract_experiment_details(experiment)

  wrapped_experiment = wrapped_experiments[experiment_name] =
    (!remock && wrapped_experiments[experiment_name]) ||
    WrappedExperiment.new(klass, experiment_name, variant_name, wrapped_experiment_chain_for(klass), [])

  wrapped_experiment.blocks << block if block
  wrapped_experiment
end