Repository navigation
Expand file tree
/
Copy pathtrial_spec.rb
More file actions
216 lines (177 loc) · 7.17 KB
/
Copy pathtrial_spec.rb
File metadata and controls
216 lines (177 loc) · 7.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
require 'spec_helper'
require 'split/trial'
describe Split::Trial do
let(:user) { Split::Persistence.adapter.new(double(session: {})) }
let(:experiment) do
Split::Experiment.new('basket_text', :alternatives => ['basket', 'cart']).save
end
it "should be initializeable" do
experiment = double('experiment')
alternative = double('alternative', :kind_of? => Split::Alternative)
trial = Split::Trial.new(:experiment => experiment, :alternative => alternative)
expect(trial.experiment).to eq(experiment)
expect(trial.alternative).to eq(alternative)
end
describe "alternative" do
it "should use the alternative if specified" do
alternative = double('alternative', :kind_of? => Split::Alternative)
trial = Split::Trial.new(:experiment => experiment = double('experiment'),
:alternative => alternative, :user => user)
expect(trial).not_to receive(:choose)
expect(trial.alternative).to eq(alternative)
end
it "should load the alternative when the alternative name is set" do
experiment = Split::Experiment.new('basket_text', :alternatives => ['basket', 'cart'])
experiment.save
trial = Split::Trial.new(:experiment => experiment, :alternative => 'basket')
expect(trial.alternative.name).to eq('basket')
end
end
describe "metadata" do
let(:alternatives) { ['basket', 'cart'] }
let(:metadata) { Hash[alternatives.map { |k| [k, "Metadata for #{k}"] }] }
let(:experiment) do
Split::Experiment.new('basket_text', :alternatives => alternatives, :metadata => metadata).save
end
it 'has metadata on each trial' do
trial = Split::Trial.new(:experiment => experiment, :user => user, :metadata => metadata['cart'],
:override => 'cart')
expect(trial.metadata).to eq(metadata['cart'])
end
it 'has metadata on each trial from the experiment' do
trial = Split::Trial.new(:experiment => experiment, :user => user)
trial.choose!
expect(trial.metadata).to eq(metadata[trial.alternative.name])
expect(trial.metadata).to match /#{trial.alternative.name}/
end
end
describe "#choose!" do
def expect_alternative(trial, alternative_name)
3.times do
trial.choose!
expect(trial.alternative.name).to eq(alternative_name)
end
end
context "when override is present" do
it "picks the override" do
trial = Split::Trial.new(:user => user, :experiment => experiment, :override => 'cart')
expect(experiment).to_not receive(:next_alternative)
expect_alternative(trial, 'cart')
end
end
context "when disabled option is true" do
it "picks the control" do
trial = Split::Trial.new(:user => user, :experiment => experiment, :disabled => true)
expect(experiment).to_not receive(:next_alternative)
expect_alternative(trial, 'basket')
end
end
context "when Split is globally disabled" do
it "picks the control" do
Split.configuration.enabled = false
trial = Split::Trial.new(:user => user, :experiment => experiment)
expect(experiment).to_not receive(:next_alternative)
expect_alternative(trial, 'basket')
Split.configuration.enabled = true
end
end
context "when experiment has winner" do
it "picks the winner" do
trial = Split::Trial.new(:user => user, :experiment => experiment)
experiment.winner = 'cart'
expect(experiment).to_not receive(:next_alternative)
expect_alternative(trial, 'cart')
end
end
context "when exclude is true" do
it "picks the control" do
trial = Split::Trial.new(:user => user, :experiment => experiment, :exclude => true)
expect(experiment).to_not receive(:next_alternative)
expect_alternative(trial, 'basket')
end
end
context "when user is already participating" do
it "picks the same alternative" do
user[experiment.key] = 'basket'
trial = Split::Trial.new(:user => user, :experiment => experiment)
expect(experiment).to_not receive(:next_alternative)
expect_alternative(trial, 'basket')
end
end
context "when user is a new participant" do
it "picks a new alternative" do
trial = Split::Trial.new(:user => user, :experiment => experiment)
expect(experiment).to receive(:next_alternative).and_call_original
trial.choose!
expect(trial.alternative.name).to_not be_empty
end
end
describe "#complete!" do
let(:trial) { Split::Trial.new(:user => user, :experiment => experiment) }
context 'when there are no goals' do
it 'should complete the trial' do
trial.choose!
old_completed_count = trial.alternative.completed_count
trial.complete!
expect(trial.alternative.completed_count).to be(old_completed_count+1)
end
end
context 'when there are many goals' do
let(:goals) { ['first', 'second'] }
let(:trial) { Split::Trial.new(:user => user, :experiment => experiment, :goals => goals) }
shared_examples_for "goal completion" do
it 'should not complete the trial' do
trial.choose!
old_completed_count = trial.alternative.completed_count
trial.complete!(goal)
expect(trial.alternative.completed_count).to_not be(old_completed_count+1)
end
end
describe 'Array of Goals' do
let(:goal) { [goals.first] }
it_behaves_like 'goal completion'
end
describe 'String of Goal' do
let(:goal) { goals.first }
it_behaves_like 'goal completion'
end
end
end
describe "alternative recording" do
before(:each) { Split.configuration.store_override = false }
context "when override is present" do
it "stores when store_override is true" do
trial = Split::Trial.new(:user => user, :experiment => experiment, :override => 'basket')
Split.configuration.store_override = true
expect(user).to receive("[]=")
trial.choose!
end
it "does not store when store_override is false" do
trial = Split::Trial.new(:user => user, :experiment => experiment, :override => 'basket')
expect(user).to_not receive("[]=")
trial.choose!
end
end
context "when disabled is present" do
it "stores when store_override is true" do
trial = Split::Trial.new(:user => user, :experiment => experiment, :disabled => true)
Split.configuration.store_override = true
expect(user).to receive("[]=")
trial.choose!
end
it "does not store when store_override is false" do
trial = Split::Trial.new(:user => user, :experiment => experiment, :disabled => true)
expect(user).to_not receive("[]=")
trial.choose!
end
end
context "when exclude is present" do
it "does not store" do
trial = Split::Trial.new(:user => user, :experiment => experiment, :exclude => true)
expect(user).to_not receive("[]=")
trial.choose!
end
end
end
end
end