correct erratically failing spec

correct erratically failing spec

diff --git a/lib/discourse_redis.rb b/lib/discourse_redis.rb
index bffba08..bcfc120 100644
--- a/lib/discourse_redis.rb
+++ b/lib/discourse_redis.rb
@@ -99,7 +99,7 @@ class DiscourseRedis
       @fallback_handler = DiscourseRedis::FallbackHandler.instance
     end
 
-    def resolve
+    def resolve(client = nil)
       if !@fallback_handler.master
         @fallback_handler.verify_master unless @fallback_handler.running?
         return @slave_options
@@ -108,7 +108,7 @@ class DiscourseRedis
       begin
         options = @options.dup
         options.delete(:connector)
-        client = Redis::Client.new(options)
+        client ||= Redis::Client.new(options)
         loading = client.call([:info]).split("\r\n").include?("loading:1")
         loading ? @slave_options : @options
       rescue Redis::ConnectionError, Redis::CannotConnectError, RuntimeError => ex
diff --git a/spec/components/discourse_redis_spec.rb b/spec/components/discourse_redis_spec.rb
index 4717529..85881ff 100644
--- a/spec/components/discourse_redis_spec.rb
+++ b/spec/components/discourse_redis_spec.rb
@@ -116,12 +116,24 @@ describe DiscourseRedis do
       end
     end
 
+    class BrokenRedis
+      def initialize(error)
+        @error = error
+      end
+
+      def call(*args)
+        raise @error
+      end
+
+      def disconnect
+      end
+    end
+
     it "should return the slave config when master's hostname cannot be resolved" do
       begin
         error = RuntimeError.new('Name or service not known')
 
-        Redis::Client.any_instance.expects(:call).raises(error).once
-        expect { connector.resolve }.to raise_error(error)
+        expect { connector.resolve(BrokenRedis.new(error)) }.to raise_error(error)
         fallback_handler.instance_variable_get(:@timer_task).shutdown
         expect(fallback_handler.running?).to eq(false)

GitHub sha: aeedecd2

Another nice example showing the pitfall of hardcore everything mocking

1 Like