diff --git a/spec/lib/appsignal/event_formatter/mongo_ruby_driver/query_formatter_spec.rb b/spec/lib/appsignal/event_formatter/mongo_ruby_driver/query_formatter_spec.rb index 82c0f3105..8bae4ef5f 100644 --- a/spec/lib/appsignal/event_formatter/mongo_ruby_driver/query_formatter_spec.rb +++ b/spec/lib/appsignal/event_formatter/mongo_ruby_driver/query_formatter_spec.rb @@ -11,8 +11,12 @@ end it "should apply a strategy for each key" do + # TODO: additional curly brackets required for issue + # https://github.com/rspec/rspec-mocks/issues/1460 + # rubocop:disable Style/BracesAroundHashParameters expect(formatter).to receive(:apply_strategy) - .with(:sanitize_document, "_id" => 1) + .with(:sanitize_document, { "_id" => 1 }) + # rubocop:enable Style/BracesAroundHashParameters expect(formatter).to receive(:apply_strategy) .with(:allow, "users") diff --git a/spec/lib/appsignal/integrations/mongo_ruby_driver_spec.rb b/spec/lib/appsignal/integrations/mongo_ruby_driver_spec.rb index e00621fa4..e4ff24b7a 100644 --- a/spec/lib/appsignal/integrations/mongo_ruby_driver_spec.rb +++ b/spec/lib/appsignal/integrations/mongo_ruby_driver_spec.rb @@ -17,8 +17,12 @@ end it "should sanitize command" do + # TODO: additional curly brackets required for issue + # https://github.com/rspec/rspec-mocks/issues/1460 + # rubocop:disable Style/BracesAroundHashParameters expect(Appsignal::EventFormatter::MongoRubyDriver::QueryFormatter) - .to receive(:format).with("find", "foo" => "bar") + .to receive(:format).with("find", { "foo" => "bar" }) + # rubocop:enable Style/BracesAroundHashParameters subscriber.started(event) end diff --git a/spec/lib/appsignal/integrations/sidekiq_spec.rb b/spec/lib/appsignal/integrations/sidekiq_spec.rb index 7a8e445e5..00a9b1386 100644 --- a/spec/lib/appsignal/integrations/sidekiq_spec.rb +++ b/spec/lib/appsignal/integrations/sidekiq_spec.rb @@ -228,10 +228,14 @@ class DelayedTestClass; end let(:error) { ExampleException } it "creates a transaction and adds the error" do + # TODO: additional curly brackets required for issue + # https://github.com/rspec/rspec-mocks/issues/1460 + # rubocop:disable Style/BracesAroundHashParameters expect(Appsignal).to receive(:increment_counter) - .with("sidekiq_queue_job_count", 1, :queue => "default", :status => :failed) + .with("sidekiq_queue_job_count", 1, { :queue => "default", :status => :failed }) expect(Appsignal).to receive(:increment_counter) - .with("sidekiq_queue_job_count", 1, :queue => "default", :status => :processed) + .with("sidekiq_queue_job_count", 1, { :queue => "default", :status => :processed }) + # rubocop:enable Style/BracesAroundHashParameters expect do perform_job { raise error, "uh oh" } @@ -267,8 +271,12 @@ class DelayedTestClass; end context "without an error" do it "creates a transaction with events" do + # TODO: additional curly brackets required for issue + # https://github.com/rspec/rspec-mocks/issues/1460 + # rubocop:disable Style/BracesAroundHashParameters expect(Appsignal).to receive(:increment_counter) - .with("sidekiq_queue_job_count", 1, :queue => "default", :status => :processed) + .with("sidekiq_queue_job_count", 1, { :queue => "default", :status => :processed }) + # rubocop:enable Style/BracesAroundHashParameters perform_job diff --git a/spec/lib/appsignal_spec.rb b/spec/lib/appsignal_spec.rb index 30ca29922..88a6c88ea 100644 --- a/spec/lib/appsignal_spec.rb +++ b/spec/lib/appsignal_spec.rb @@ -427,49 +427,89 @@ end describe ".monitor_single_transaction" do + around { |example| keep_transactions { example.run } } + context "with a successful call" do - it "should call monitor_transaction and stop" do - expect(Appsignal).to receive(:monitor_transaction).with( - "perform_job.something", - :key => :value - ).and_yield + it "calls monitor_transaction and Appsignal.stop" do expect(Appsignal).to receive(:stop) - Appsignal.monitor_single_transaction("perform_job.something", :key => :value) do + Appsignal.monitor_single_transaction( + "perform_job.something", + :controller => :my_controller, + :action => :my_action + ) do # nothing end + + transaction = last_transaction + transaction_hash = transaction.to_h + expect(transaction_hash).to include( + "action" => "my_controller#my_action" + ) + expect(transaction_hash["events"]).to match([ + hash_including( + "name" => "perform_job.something", + "title" => "", + "body" => "", + "body_format" => Appsignal::EventFormatter::DEFAULT + ) + ]) end end context "with an erroring call" do let(:error) { ExampleException.new } - it "should call monitor_transaction and stop and then raise the error" do - expect(Appsignal).to receive(:monitor_transaction).with( - "perform_job.something", - :key => :value - ).and_yield + it "calls monitor_transaction and stop and re-raises the error" do expect(Appsignal).to receive(:stop) expect do - Appsignal.monitor_single_transaction("perform_job.something", :key => :value) do + Appsignal.monitor_single_transaction( + "perform_job.something", + :controller => :my_controller, + :action => :my_action + ) do raise error end end.to raise_error(error) + + transaction = last_transaction + transaction_hash = transaction.to_h + expect(transaction_hash).to include( + "action" => "my_controller#my_action" + ) + expect(transaction_hash["events"]).to match([ + hash_including( + "name" => "perform_job.something", + "title" => "", + "body" => "", + "body_format" => Appsignal::EventFormatter::DEFAULT + ) + ]) end end end describe ".tag_request" do - before { allow(Appsignal::Transaction).to receive(:current).and_return(transaction) } + let(:transaction) { http_request_transaction } + around do |example| + start_agent + with_current_transaction transaction do + keep_transactions { example.run } + end + end context "with transaction" do - let(:transaction) { double } - it "should call set_tags on transaction" do - expect(transaction).to receive(:set_tags).with("a" => "b") - end + it "calls set_tags on the current transaction" do + Appsignal.tag_request("a" => "b") + transaction.complete # Manually trigger transaction sampling - after { Appsignal.tag_request("a" => "b") } + expect(transaction.to_h).to include( + "sample_data" => hash_including( + "tags" => { "a" => "b" } + ) + ) + end end context "without transaction" do diff --git a/spec/support/helpers/transaction_helpers.rb b/spec/support/helpers/transaction_helpers.rb index f5cf20948..85e10284c 100644 --- a/spec/support/helpers/transaction_helpers.rb +++ b/spec/support/helpers/transaction_helpers.rb @@ -56,6 +56,16 @@ def clear_current_transaction! Thread.current[:appsignal_transaction] = nil end + # Set the current for the duration of the given block. + # + # Helper for {set_current_transaction} and {clear_current_transaction!} + def with_current_transaction(transaction) + set_current_transaction transaction + yield + ensure + clear_current_transaction! + end + # Track the AppSignal transaction JSON when a transaction gets completed # ({Appsignal::Transaction.complete}). #