-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #187 from alphagov/metrics-test
Add unit tests to `Metrics` module
- Loading branch information
Showing
1 changed file
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
RSpec.describe Metrics do | ||
describe ".increment_counter" do | ||
let(:counter) { double(observe: nil) } | ||
|
||
before do | ||
stub_const("Metrics::COUNTERS", { foo: counter }) | ||
end | ||
|
||
it "observes an increment of 1 for the given counter with the given labels" do | ||
described_class.increment_counter(:foo, bar: "baz") | ||
|
||
expect(counter).to have_received(:observe).with(1, bar: "baz") | ||
end | ||
|
||
it "fails gracefully if the counter does not exist" do | ||
expect { described_class.increment_counter(:bar) }.not_to raise_error | ||
end | ||
|
||
it "fails gracefully if the operation raises an error" do | ||
allow(counter).to receive(:observe).and_raise("boom") | ||
|
||
expect { described_class.increment_counter(:foo) }.not_to raise_error | ||
end | ||
end | ||
|
||
describe ".observe_duration" do | ||
let(:histogram) { double(observe: nil) } | ||
|
||
before do | ||
stub_const("Metrics::HISTOGRAMS", { foo: histogram }) | ||
end | ||
|
||
it "observes the duration of the given block for the given histogram with the given labels" do | ||
described_class.observe_duration(:foo, bar: "baz") { "result" } | ||
|
||
expect(histogram).to have_received(:observe).with(kind_of(Float), bar: "baz") | ||
end | ||
|
||
it "returns the result of the given block" do | ||
expect(described_class.observe_duration(:foo) { "result" }).to eq("result") | ||
end | ||
|
||
it "fails gracefully if the histogram does not exist" do | ||
expect(described_class.observe_duration(:bar) { "result" }).to eq("result") | ||
end | ||
|
||
it "fails gracefully if the operation raises an error" do | ||
allow(histogram).to receive(:observe).and_raise("boom") | ||
|
||
expect(described_class.observe_duration(:bar) { "result" }).to eq("result") | ||
end | ||
end | ||
end |