Skip to content

Commit 07b71ce

Browse files
eliotsykesandrew
authored andcommitted
Document testing with RSpec (#495)
1 parent 15fe41d commit 07b71ce

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,36 @@ In the event you want to disable all tests without having to know the individual
150150

151151
It is not required to send `SPLIT_DISABLE=false` to activate Split.
152152

153+
To aid testing with RSpec, write `split_helper.rb` and call `use_ab_test(alternatives_by_experiment)` in your specs as instructed below:
154+
155+
```ruby
156+
# Recommended path for this file is 'spec/support/split_helper.rb', and you will need to ensure it
157+
# is `require`-d by rails_helper.rb or spec_helper.rb
158+
module SplitHelper
159+
160+
# Usage:
161+
#
162+
# Force a specific experiment alternative to always be returned:
163+
# use_ab_test(signup_form: "single_page")
164+
#
165+
# Force alternatives for multiple experiments:
166+
# use_ab_test(signup_form: "single_page", pricing: "show_enterprise_prices")
167+
#
168+
def use_ab_test(alternatives_by_experiment)
169+
allow_any_instance_of(Split::Helper).to receive(:ab_test) do |_receiver, experiment|
170+
alternative =
171+
alternatives_by_experiment.fetch(experiment) { |key| raise "Unknown experiment '#{key}'" }
172+
end
173+
end
174+
end
175+
176+
RSpec.configure do |config|
177+
# Make the `use_ab_test` method available to all specs:
178+
config.include SplitHelper
179+
end
180+
```
181+
182+
153183
### Starting experiments manually
154184

155185
By default new A/B tests will be active right after deployment. In case you would like to start new test a while after

0 commit comments

Comments
 (0)