From 4445631243a7a8066562489d25b0fc44c8085c82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Luis=20Leal=20Cardoso=20Junior?= Date: Sun, 3 Mar 2024 11:01:58 -0300 Subject: [PATCH 1/2] Refactor spec/encapsulated_helper_spec.rb to not include EncapsuledHelper on itself --- spec/encapsulated_helper_spec.rb | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) diff --git a/spec/encapsulated_helper_spec.rb b/spec/encapsulated_helper_spec.rb index ba04683a..c44b2d62 100644 --- a/spec/encapsulated_helper_spec.rb +++ b/spec/encapsulated_helper_spec.rb @@ -3,7 +3,7 @@ require "spec_helper" describe Split::EncapsulatedHelper do - include Split::EncapsulatedHelper + let(:context_shim) { Split::EncapsulatedHelper::ContextShim.new(double(request: request)) } describe "ab_test" do before do @@ -11,26 +11,15 @@ .and_return(mock_user) end - context "when params raises an error" do - before do - allow(self).to receive(:params).and_raise(NoMethodError) - end - - it "should not raise an error " do - expect { params }.to raise_error(NoMethodError) - expect { ab_test("link_color", "blue", "red") }.not_to raise_error - end - end - it "calls the block with selected alternative" do - expect { |block| ab_test("link_color", "red", "red", &block) }.to yield_with_args("red", {}) + expect { |block| context_shim.ab_test("link_color", "red", "red", &block) }.to yield_with_args("red", {}) end context "inside a view" do it "works inside ERB" do require "erb" template = ERB.new(<<-ERB.split(/\s+/s).map(&:strip).join(" "), nil, "%") - foo <% ab_test(:foo, '1', '2') do |alt, meta| %> + foo <% context_shim.ab_test(:foo, '1', '2') do |alt, meta| %> static <%= alt %> <% end %> ERB From 397dc477422b87a21016ce11a2f704102bbd5c65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Luis=20Leal=20Cardoso=20Junior?= Date: Sun, 3 Mar 2024 11:02:47 -0300 Subject: [PATCH 2/2] We can just check if request/params are there --- lib/split/encapsulated_helper.rb | 2 +- lib/split/helper.rb | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/split/encapsulated_helper.rb b/lib/split/encapsulated_helper.rb index 9dd1750f..ab48ea23 100644 --- a/lib/split/encapsulated_helper.rb +++ b/lib/split/encapsulated_helper.rb @@ -24,7 +24,7 @@ def initialize(context) end def params - request.params if request_present? + request.params if request && request.respond_to?(:params) end def request diff --git a/lib/split/helper.rb b/lib/split/helper.rb index e80c1039..5e1b2b7f 100644 --- a/lib/split/helper.rb +++ b/lib/split/helper.rb @@ -163,11 +163,11 @@ def is_ignored_ip_address? end def params_present? - defined?(params) && params != nil + defined?(params) && params end def request_present? - defined?(request) && request != nil + defined?(request) && request end def active_experiments