Module: Gitlab::Experiment::RSpecHelpers

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

Instance Method Summary collapse

Instance Method Details

#stub_experiments(experiments) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/gitlab/experiment/rspec.rb', line 6

def stub_experiments(experiments)
  experiments.each do |name, variant|
    variant = :control if variant == false

    base = Configuration.base_class.constantize
    klass = base.constantize(name) || base

    # We have to use this high level any_instance behavior as there's
    # not an alternative that allows multiple wrappings of `new`.
    allow_any_instance_of(klass).to receive(:enabled?).and_return(true)

    if variant == true # passing true allows the rollout to do its job
      allow_any_instance_of(klass).to receive(:experiment_group?).and_return(true)
    else
      allow_any_instance_of(klass).to receive(:resolve_variant_name).and_return(variant.to_s)
    end
  end
end

#wrapped_experiment(experiment, shallow: false, failure: nil, &block) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/gitlab/experiment/rspec.rb', line 25

def wrapped_experiment(experiment, shallow: false, failure: nil, &block)
  if shallow
    yield experiment if block.present?
    return experiment
  end

  receive_wrapped_new = receive(:new).and_wrap_original do |new, *new_args, &new_block|
    instance = new.call(*new_args)
    instance.tap(&block) if block.present?
    instance.tap(&new_block) if new_block.present?
    instance
  end

  klass = experiment.class == Class ? experiment : experiment.class
  if failure
    expect(klass).to receive_wrapped_new, failure
  else
    allow(klass).to receive_wrapped_new
  end
end