Skip to content

Commit

Permalink
Tweak RSpec instructions (#540)
Browse files Browse the repository at this point in the history
  • Loading branch information
eliotsykes authored and andrew committed Sep 8, 2018
1 parent 3fbf1be commit 03eab94
Showing 1 changed file with 12 additions and 16 deletions.
28 changes: 12 additions & 16 deletions README.md
Expand Up @@ -161,43 +161,39 @@ It is not required to send `SPLIT_DISABLE=false` to activate Split.


### Rspec Helper
To aid testing with RSpec, write `split_helper.rb` and call `use_ab_test(alternatives_by_experiment)` in your specs as instructed below:
To aid testing with RSpec, write `spec/support/split_helper.rb` and call `use_ab_test(alternatives_by_experiment)` in your specs as instructed below:

```ruby
# Recommended path for this file is 'spec/support/split_helper.rb'
# Create a file with these contents at 'spec/support/split_helper.rb'
# and ensure it is `require`d in your rails_helper.rb or spec_helper.rb
module SplitHelper

# Force a specific experiment alternative to always be returned:
# use_ab_test(signup_form: "single_page")
#
# Force alternatives for multiple experiments:
# use_ab_test(signup_form: "single_page", pricing: "show_enterprise_prices")
#
def use_ab_test(alternatives_by_experiment)
allow_any_instance_of(Split::Helper).to receive(:ab_test) do |_receiver, experiment|
alternatives_by_experiment.fetch(experiment) { |key| raise "Unknown experiment '#{key}'" }
alternatives_by_experiment.fetch(experiment) { |key| raise "Unknown experiment '#{key}'" }
end
end
end
```

Needs to ensure it `required` by rails_helper.rb or spec_helper.rb
```ruby
# Make the `use_ab_test` method available to all specs:
RSpec.configure do |config|
config.include SplitHelper
end
```

Example:
Now you can call `use_ab_test(alternatives_by_experiment)` in your specs, for example:
```ruby
it "registers using experimental signup" do

use_ab_test experiment_name: "alternative_name"

post "/signups"

...
end

# Force a specific experiment alternative to always be returned:
# use_ab_test(signup_form: "single_page")
#
# Force alternatives for multiple experiments:
# use_ab_test(signup_form: "single_page", pricing: "show_enterprise_prices")
```


Expand Down

0 comments on commit 03eab94

Please sign in to comment.