require 'spec_helper' describe WebhookStopwatch::Client do let(:redis_url) { "redis://127.0.0.1:6379/5" } let(:redis) { mock("Redis", :publish => 1) } subject { WebhookStopwatch::Client.new(redis_url) } before(:each) do Redis.stub(:new).and_return(redis) end describe "#start" do it "can take a redis connection as the initialize instead of url" do Redis.should_not_receive(:new) redis.should_receive(:publish). with("webhook_stopwatch:events", Oj.dump({"message_id" => 123, "timestamp" => 1000000, "event" => "start"})) WebhookStopwatch::Client.new(redis).start(123, 1000000) end it "configures redis" do Redis.should_receive(:new).with(:url => "redis://127.0.0.1:6379/5") subject.start(123, 1000000) end it "publishes a message to redis" do redis.should_receive(:publish). with("webhook_stopwatch:events", Oj.dump({"message_id" => 123, "timestamp" => 1000000, "event" => "start"})) subject.start(123, 1000000) end it "returns true" do subject.start(123, 1000000).should == true end context "all hell breaks loose" do before(:each) do redis.stub(:publish).and_raise("oh no") end it "returns false" do subject.start(123, 1000000).should == false end end end describe "#stop" do it "can take a redis connection as the initialize instead of url" do Redis.should_not_receive(:new) redis.should_receive(:publish). with("webhook_stopwatch:events", Oj.dump({"message_id" => 123, "timestamp" => 1000000, "event" => "stop"})) WebhookStopwatch::Client.new(redis).stop(123, 1000000) end it "configures redis" do Redis.should_receive(:new).with(:url => "redis://127.0.0.1:6379/5") subject.start(123, 1000000) end it "publishes a message to redis" do redis.should_receive(:publish). with("webhook_stopwatch:events", Oj.dump({"message_id" => 123, "timestamp" => 1000000, "event" => "stop"})) subject.stop(123, 1000000) end it "returns true" do subject.stop(123, 1000000).should == true end context "all hell breaks loose" do before(:each) do redis.stub(:publish).and_raise("oh no") end it "returns false" do subject.stop(123, 1000000).should == false end end end describe "#ping" do it "sends an empty event" do redis.should_receive(:publish).with("webhook_stopwatch:events", "{}") subject.ping end it "returns the subscriber count" do subject.ping.should == 1 end end end