spec/lib/coach/notifications_spec.rb in coach-0.5.2 vs spec/lib/coach/notifications_spec.rb in coach-1.0.0

- old
+ new

@@ -1,38 +1,39 @@ -require 'spec_helper' -require 'coach/notifications' +require "spec_helper" +require "coach/notifications" describe Coach::Notifications do subject(:notifications) { described_class.instance } - before { described_class.unsubscribe! } - # Remove need to fully mock a request object before do + described_class.unsubscribe! + + # Remove need to fully mock a request object allow(Coach::RequestSerializer). - to receive(:new).and_return(double(serialize: {})) + to receive(:new). + and_return(instance_double("Coach::RequestSerializer", serialize: {})) + + ActiveSupport::Notifications.subscribe(/\.coach$/) do |name, *_, event| + events << [name, event] + end end # Capture all Coach events let(:events) { [] } let(:middleware_event) do - event = events.find { |(name, _)| name == 'coach.request' } + event = events.find { |(name, _)| name == "request.coach" } event && event[1] end - before do - ActiveSupport::Notifications.subscribe(/coach/) do |name, *_, event| - events << [name, event] - end - end # Mock a handler to simulate an endpoint call let(:handler) do - middleware_a = build_middleware('A') - middleware_b = build_middleware('B') + middleware_a = build_middleware("A") + middleware_b = build_middleware("B") middleware_a.uses(middleware_b) - terminal_middleware = build_middleware('Terminal') + terminal_middleware = build_middleware("Terminal") terminal_middleware.uses(middleware_a) Coach::Handler.new(terminal_middleware) end @@ -41,41 +42,41 @@ it "becomes active" do expect(notifications.active?).to be(true) end - it "will now send coach.request" do + it "will now send request.coach" do handler.call({}) - expect(middleware_event).not_to be_nil + expect(middleware_event).to_not be_nil end - describe "coach.request event" do + describe "request.coach event" do before { handler.call({}) } it "contains all middleware that have been run" do middleware_names = middleware_event[:chain].map { |item| item[:name] } - expect(middleware_names).to include('Terminal', 'A', 'B') + expect(middleware_names).to include("Terminal", "A", "B") end it "includes all logged metadata" do expect(middleware_event). to include(metadata: { A: true, B: true, Terminal: true }) end end end describe "#unsubscribe!" do - it "should disable any prior subscriptions" do + it "disables any prior subscriptions" do notifications.subscribe! handler.call({}) - expect(events.count { |(name, _)| name == 'coach.request' }). + expect(events.count { |(name, _)| name == "request.coach" }). to eq(1) notifications.unsubscribe! handler.call({}) - expect(events.count { |(name, _)| name == 'coach.request' }). + expect(events.count { |(name, _)| name == "request.coach" }). to eq(1) end end end