Testing Utilities
MiniAPM provides testing utilities to verify that your application is correctly instrumented.
Require the testing helpers:
# spec/spec_helper.rb or test/test_helper.rbrequire 'miniapm/testing'RSpec Integration
Section titled “RSpec Integration”Use the :miniapm metadata tag:
RSpec.describe OrdersController, :miniapm do it "tracks the request" do get "/orders"
expect(MiniAPM::Testing.spans).to include( having_attributes(name: /process_action/) ) endendOr enable globally:
RSpec.configure do |config| config.include MiniAPM::Testing::RSpecHelpers, type: :requestendMinitest Integration
Section titled “Minitest Integration”class OrdersControllerTest < ActionDispatch::IntegrationTest include MiniAPM::Testing::MinitestHelpers
# Enable for all tests in this class enable_miniapm!
def test_tracks_request get "/orders"
assert MiniAPM::Testing.find_span(name: /process_action/) endendTesting Helpers
Section titled “Testing Helpers”Recorded Spans
Section titled “Recorded Spans”Access all recorded spans:
# Get all spansMiniAPM::Testing.spans
# Find spans by nameMiniAPM::Testing.find_spans(name: /ActiveRecord/)
# Find single spanMiniAPM::Testing.find_span(name: "process_action")Recorded Errors
Section titled “Recorded Errors”Access captured errors:
# Get all errorsMiniAPM::Testing.errors
# Find errors by typeMiniAPM::Testing.find_errors(type: "RuntimeError")
# Find single errorMiniAPM::Testing.find_error(message: /something went wrong/)Clearing State
Section titled “Clearing State”Clear recorded data between tests:
# Automatically done when using :miniapm tag# Or manually:MiniAPM::Testing.clear!Example Test Cases
Section titled “Example Test Cases”Testing Request Tracing
Section titled “Testing Request Tracing”RSpec.describe "Orders API", :miniapm do it "creates spans for HTTP requests" do get "/api/orders"
span = MiniAPM::Testing.find_span(name: /process_action/)
expect(span).to be_present expect(span.attributes["http.method"]).to eq("GET") expect(span.attributes["http.status_code"]).to eq(200) endendTesting Database Queries
Section titled “Testing Database Queries”RSpec.describe "Database queries", :miniapm do it "creates spans for SQL queries" do Order.all.to_a
spans = MiniAPM::Testing.find_spans(name: /sql/)
expect(spans).not_to be_empty expect(spans.first.attributes["db.system"]).to eq("postgresql") endendTesting Error Capture
Section titled “Testing Error Capture”RSpec.describe "Error handling", :miniapm do it "captures exceptions" do expect { get "/trigger_error" }.to raise_error(RuntimeError)
error = MiniAPM::Testing.find_error(type: "RuntimeError")
expect(error).to be_present expect(error.message).to include("Something went wrong") expect(error.backtrace).to be_present endendTesting Custom Spans
Section titled “Testing Custom Spans”RSpec.describe "Custom instrumentation", :miniapm do it "records custom spans" do MiniAPM.span("process_payment") do |span| span.add_attribute("payment.amount", 100) # ... payment logic end
span = MiniAPM::Testing.find_span(name: "process_payment")
expect(span).to be_present expect(span.attributes["payment.amount"]).to eq(100) endendTesting Background Jobs
Section titled “Testing Background Jobs”RSpec.describe ProcessOrderJob, :miniapm do it "traces job execution" do perform_enqueued_jobs do ProcessOrderJob.perform_later(order_id: 123) end
span = MiniAPM::Testing.find_span(name: /ProcessOrderJob/)
expect(span).to be_present expect(span.attributes["job.queue"]).to eq("default") endendAsserting Span Attributes
Section titled “Asserting Span Attributes”RSpec.describe "Span attributes", :miniapm do it "includes expected attributes" do get "/orders/123"
span = MiniAPM::Testing.find_span(name: /process_action/)
expect(span.attributes).to include( "http.method" => "GET", "http.route" => "OrdersController#show", "http.status_code" => 200 ) endendAsserting No Spans/Errors
Section titled “Asserting No Spans/Errors”RSpec.describe "Filtered paths", :miniapm do it "does not trace health checks" do get "/health"
spans = MiniAPM::Testing.find_spans(name: /health/)
expect(spans).to be_empty endend