Skip to content

Commit 8a8a04d

Browse files
authored
Refactor test suite with transaction helpers (#1131)
Make the assertions easier to read by describing what we want to see happen, rather than assert some Hash of internal representation of the transaction. Part of #252 Part of #299 [skip changeset]
1 parent 32143c8 commit 8a8a04d

34 files changed

+1399
-1952
lines changed

spec/lib/appsignal/demo_spec.rb

Lines changed: 38 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -32,56 +32,53 @@
3232
end
3333

3434
describe ".create_example_error_request" do
35-
let!(:error_transaction) { http_request_transaction }
36-
let(:config) { project_fixture_config("production") }
37-
before do
38-
Appsignal.config = config
39-
expect(Appsignal::Transaction).to receive(:new).with(
40-
kind_of(String),
41-
Appsignal::Transaction::HTTP_REQUEST,
42-
kind_of(::Rack::Request),
43-
kind_of(Hash)
44-
).and_return(error_transaction)
45-
end
46-
subject { described_class.send(:create_example_error_request) }
35+
before { start_agent }
36+
around { |example| keep_transactions { example.run } }
4737

4838
it "sets an error" do
49-
expect(error_transaction).to receive(:set_error).with(kind_of(described_class::TestError))
50-
expect(error_transaction).to receive(:set_metadata).with("path", "/hello")
51-
expect(error_transaction).to receive(:set_metadata).with("method", "GET")
52-
expect(error_transaction).to receive(:set_metadata).with("demo_sample", "true")
53-
expect(error_transaction).to receive(:complete)
54-
subject
39+
described_class.send(:create_example_error_request)
40+
41+
transaction = last_transaction
42+
expect(transaction).to have_id
43+
expect(transaction).to have_namespace(Appsignal::Transaction::HTTP_REQUEST)
44+
expect(transaction).to have_action("DemoController#hello")
45+
expect(transaction).to have_error(
46+
"Appsignal::Demo::TestError",
47+
"Hello world! This is an error used for demonstration purposes."
48+
)
49+
expect(transaction).to include_metadata(
50+
"path" => "/hello",
51+
"method" => "GET",
52+
"demo_sample" => "true"
53+
)
54+
expect(transaction).to be_completed
5555
end
5656
end
5757

5858
describe ".create_example_performance_request" do
59-
let!(:performance_transaction) { http_request_transaction }
60-
let(:config) { project_fixture_config("production") }
61-
before do
62-
Appsignal.config = config
63-
expect(Appsignal::Transaction).to receive(:new).with(
64-
kind_of(String),
65-
Appsignal::Transaction::HTTP_REQUEST,
66-
kind_of(::Rack::Request),
67-
kind_of(Hash)
68-
).and_return(performance_transaction)
69-
end
70-
subject { described_class.send(:create_example_performance_request) }
59+
before { start_agent }
60+
around { |example| keep_transactions { example.run } }
7161

7262
it "sends a performance sample" do
73-
expect(performance_transaction).to receive(:start_event)
74-
expect(performance_transaction).to receive(:finish_event).with(
75-
"action_view.render",
76-
"Render hello.html.erb",
77-
"<h1>Hello world!</h1>",
78-
Appsignal::EventFormatter::DEFAULT
63+
described_class.send(:create_example_performance_request)
64+
65+
transaction = last_transaction
66+
expect(transaction).to have_id
67+
expect(transaction).to have_namespace(Appsignal::Transaction::HTTP_REQUEST)
68+
expect(transaction).to have_action("DemoController#hello")
69+
expect(transaction).to_not have_error
70+
expect(transaction).to include_metadata(
71+
"path" => "/hello",
72+
"method" => "GET",
73+
"demo_sample" => "true"
74+
)
75+
expect(transaction).to include_event(
76+
"name" => "action_view.render",
77+
"title" => "Render hello.html.erb",
78+
"body" => "<h1>Hello world!</h1>",
79+
"body_format" => Appsignal::EventFormatter::DEFAULT
7980
)
80-
expect(performance_transaction).to receive(:set_metadata).with("path", "/hello")
81-
expect(performance_transaction).to receive(:set_metadata).with("method", "GET")
82-
expect(performance_transaction).to receive(:set_metadata).with("demo_sample", "true")
83-
expect(performance_transaction).to receive(:complete)
84-
subject
81+
expect(transaction).to be_completed
8582
end
8683
end
8784
end

0 commit comments

Comments
 (0)