Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve testing modes #26

Merged
merged 2 commits into from
Jun 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,26 @@ BigQuery (respectively) out of the loop.

### 4. Adding specs

The `dfe-analytics` gem comes with an RSpec matcher that can be used to ensure
that an integration exists in controllers and models. The RSpec matcher file
needs to be required into specs, and provides two different styles of matchers
to use:
#### Testing modes

``` ruby
The `dfe-analytics` Gem comes with a testing mode which prevents real analytics from being recorded when running tests.

```ruby
require 'dfe/analytics/testing'

DfE::Analytics::Testing.fake!

DfE::Analytics::Testing.webmock!
```

- `fake!` is the default mode, and this effectively stubs the BigQuery client meaning no requests are made.
- `webmock!` makes the library act as normal, allowing you to write tests against mocked requests.

#### Matchers

The Gem also comes with an RSpec matcher that can be used to ensure that an integration exists in controllers and models. The RSpec matcher file needs to be required into specs, and provides two different styles of matchers to use:

```ruby
require 'dfe/analytics/rspec/matchers'

# have_sent_analytics_event_types take a block and expects event types to be sent
Expand Down
2 changes: 1 addition & 1 deletion lib/dfe/analytics/testing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def switch_test_mode(test_mode)
end

class StubClient
def insert(_events)
def insert(*)
true
end
end
Expand Down
11 changes: 11 additions & 0 deletions spec/dfe/analytics/send_events_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@
end
end

context 'when using fake testing mode' do
it 'does not go out to the network' do
request = stub_analytics_event_submission

DfE::Analytics::Testing.fake! do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This block isn't necessary as it's the default state, set in the body of the Testing module (also a la sidekiq) — could rename spec and/or add something to the docs?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah it's true that this is default, but I wanted this test to be explicit in case that changes in the future. It's clear that whatever might change around this test, we're at least using the fake testing mode in it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK I'm with you

described_class.new.perform([event.as_json])
expect(request).not_to have_been_made
end
end
end

describe 'retry behaviour' do
before do
# we don't want to define a permanent exception, just one for this test
Expand Down