Skip to content

Commit

Permalink
Add testing guide.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Apr 9, 2024
1 parent b1d7b61 commit e915c9a
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 1 deletion.
16 changes: 16 additions & 0 deletions examples/testing/app.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require 'metrics/provider'

class MyApplication
def work
# ...
end

Metrics::Provider(self) do
WORK_METRIC = Metrics.metric('my_application.work.count', :counter, description: 'Work counter')

def work
WORK_METRIC.emit(1)
super
end
end
end
12 changes: 12 additions & 0 deletions examples/testing/test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env sus

ENV['METRICS_BACKEND'] ||= 'metrics/backend/test'

require_relative 'app'

describe MyApplication do
it 'should emit metrics' do
expect(MyApplication::WORK_METRIC).to receive(:emit).with(1)
MyApplication.new.work
end
end
4 changes: 3 additions & 1 deletion guides/links.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
getting-started:
order: 1
capture:
order: 2
order: 2
testing:
order: 3
43 changes: 43 additions & 0 deletions guides/testing/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Testing

This guide explains how to write assertions in your test suite to validate `metrics` are being emitted correctly.

## Application Code

In your application code, you should emit metrics, e.g.

```ruby
require 'metrics/provider'

class MyApplication
def work
# ...
end

Metrics::Provider(self) do
WORK_METRIC = Metrics.metric('my_application.work.count', :counter, description: 'Work counter')

def work
WORK_METRIC.emit(1)
super
end
end
end
```

## Test Code

In your test code, you should assert that the metrics are being emitted correctly, e.g.

```ruby
ENV['METRICS_BACKEND'] ||= 'metrics/backend/test'

require_relative 'app'

describe MyApplication do
it 'should emit metrics' do
expect(MyApplication::WORK_METRIC).to receive(:emit).with(1)
MyApplication.new.work
end
end
```

0 comments on commit e915c9a

Please sign in to comment.