Sha256: 1408e355fd4fcf28da31b072493df33c15416cf3af359cb4002d5d1dc2aaf2d3

Contents?: true

Size: 2 KB

Versions: 3

Compression:

Stored size: 2 KB

Contents

require 'airbrake/rails/active_record_subscriber'

RSpec.describe Airbrake::Rails::ActiveRecordSubscriber do
  after { Airbrake::Rack::RequestStore.clear }

  describe "#call" do
    let(:event) { double(Airbrake::Rails::Event) }

    before do
      allow(Airbrake::Rails::Event).to receive(:new).and_return(event)
    end

    context "when there are no routes in the request store" do
      it "doesn't notify requests" do
        expect(Airbrake).not_to receive(:notify_query)
        subject.call([])
      end
    end

    context "when there's a route in the request store" do
      before do
        Airbrake::Rack::RequestStore[:routes] = {
          '/test-route' => { method: 'GET', response_type: :html }
        }

        allow(event).to receive(:sql).and_return('SELECT * FROM bananas')
        allow(event).to receive(:time).and_return(Time.now)
        allow(event).to receive(:end).and_return(Time.now)
        allow(Airbrake::Rails::BacktraceCleaner).to receive(:clean).and_return(
          "/lib/pry/cli.rb:117:in `start'"
        )
      end

      it "sends query info to Airbrake" do
        expect(Airbrake).to receive(:notify_query).with(
          hash_including(
            method: 'GET',
            route: '/test-route',
            query: 'SELECT * FROM bananas',
            func: 'start',
            line: 117,
            file: '/lib/pry/cli.rb'
          )
        )
        subject.call([])
      end

      context "and when backtrace couldn't be parsed" do
        before do
          allow(Airbrake::Rails::BacktraceCleaner)
            .to receive(:clean).and_return([])
        end

        it "sends empty func/file/line to Airbrake" do
          expect(Airbrake).to receive(:notify_query).with(
            hash_including(
              method: 'GET',
              route: '/test-route',
              query: 'SELECT * FROM bananas',
              func: nil,
              line: nil,
              file: nil
            )
          )
          subject.call([])
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
airbrake-9.2.1 spec/unit/rails/active_record_subscriber_spec.rb
airbrake-9.2.0 spec/unit/rails/active_record_subscriber_spec.rb
airbrake-9.1.0 spec/unit/rails/active_record_subscriber_spec.rb