Skip to content

Commit f34f68d

Browse files
authored
Merge pull request #723 from splitrb/fix-context-shim-override-behavior
Fix context shim override behavior
2 parents 7a075be + 397dc47 commit f34f68d

File tree

3 files changed

+6
-17
lines changed

3 files changed

+6
-17
lines changed

lib/split/encapsulated_helper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def initialize(context)
2424
end
2525

2626
def params
27-
request.params if request_present?
27+
request.params if request && request.respond_to?(:params)
2828
end
2929

3030
def request

lib/split/helper.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,11 +163,11 @@ def is_ignored_ip_address?
163163
end
164164

165165
def params_present?
166-
defined?(params) && params != nil
166+
defined?(params) && params
167167
end
168168

169169
def request_present?
170-
defined?(request) && request != nil
170+
defined?(request) && request
171171
end
172172

173173
def active_experiments

spec/encapsulated_helper_spec.rb

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,23 @@
33
require "spec_helper"
44

55
describe Split::EncapsulatedHelper do
6-
include Split::EncapsulatedHelper
6+
let(:context_shim) { Split::EncapsulatedHelper::ContextShim.new(double(request: request)) }
77

88
describe "ab_test" do
99
before do
1010
allow_any_instance_of(Split::EncapsulatedHelper::ContextShim).to receive(:ab_user)
1111
.and_return(mock_user)
1212
end
1313

14-
context "when params raises an error" do
15-
before do
16-
allow(self).to receive(:params).and_raise(NoMethodError)
17-
end
18-
19-
it "should not raise an error " do
20-
expect { params }.to raise_error(NoMethodError)
21-
expect { ab_test("link_color", "blue", "red") }.not_to raise_error
22-
end
23-
end
24-
2514
it "calls the block with selected alternative" do
26-
expect { |block| ab_test("link_color", "red", "red", &block) }.to yield_with_args("red", {})
15+
expect { |block| context_shim.ab_test("link_color", "red", "red", &block) }.to yield_with_args("red", {})
2716
end
2817

2918
context "inside a view" do
3019
it "works inside ERB" do
3120
require "erb"
3221
template = ERB.new(<<-ERB.split(/\s+/s).map(&:strip).join(" "), nil, "%")
33-
foo <% ab_test(:foo, '1', '2') do |alt, meta| %>
22+
foo <% context_shim.ab_test(:foo, '1', '2') do |alt, meta| %>
3423
static <%= alt %>
3524
<% end %>
3625
ERB

0 commit comments

Comments
 (0)