-
Notifications
You must be signed in to change notification settings - Fork 7
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
add have_sent_analytics_event rspec matcher #11
Conversation
As discussed with @duncanjbrown, we're going to try to change this so that instead of pulling this into the generator as a template, the app that wants to use this matcher will have to require it into the spec that uses the matcher. |
494ce5a
to
4b03c48
Compare
75e6c3b
to
d6ee7d6
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
event_types.each do |event_type| | ||
expect(@enqueued_event_types).to include(event_type.to_s) | ||
end | ||
# expect(event_types.map(&:to_s) & @enqueued_event_types).not_to be_blank |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Stray comment
lib/dfe/analytics/rspec/matchers/have_sent_analytics_event_types.rb
Outdated
Show resolved
Hide resolved
# Parsing the job args above makes a couple of assumptions that may | ||
# raise an error. Treat these as non-analytics events by returning | ||
# 'nil' for the type. | ||
nil |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we log here, or are all errors definitely to be ignored?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think they can safely be ignored. I can't see a way to be tripped up by this when using the matcher in your spec. The only way to trip up that comes to mind is that if you're changing the matcher code, you might be a little bit surprised if you have an error in there, but I think that's rare and that if it happens, it won't be hard to debug when you realise that all the event_types
returned are nil
.
Do you think that's good enough justification to swallow errors here?
it 'passes when the given event type was triggered' do | ||
DfE::Analytics::SendEvents.do([web_request_event.as_json]) | ||
|
||
expect(:web_request).to have_been_enqueued_event_types |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This reads a bit oddly — could it be
expect(:web_request).to have_been_enqueued_for_analytics
or sth?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You've made this comment twice. Just to say I like this suggestion better, and am commenting on the other below. ignore this comment, see the thesis below.
it 'passes when the given event type was triggered' do | ||
DfE::Analytics::SendEvents.do([web_request_event.as_json]) | ||
|
||
expect(:web_request).to have_been_enqueued_event_types |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This reads a bit oddly — could it be
expect_analytics_event_to_have_been_enqueued(:web_request)
or even
expect_analytics_web_request_event_to_have_been_enqueued # etc...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree that this reads oddly, I was justifying doing it this way by looking at existing RSpec matchers have_been_enqueued
. It seemed to me that this would provide for the least surprise for users ... but then, maybe you're demonstrating that that's not true? What do you think in light of the existing RSpec matchers?
re: expect_analytics_web_request_event_to_have_been_enqueued
I did play with having have_sent_request_analytics_event_types
and have_sent_entities_analytics_event_types
, but that added complexity at a different levels and I just removed them. Rubocop was whinging about having them in the same file as the have_sent_analytics_event_types
, and they wouldn't be usable in feature tests such as the ones I was writing for Publish which would expect both web_requests
and one of the entity events.
def then_it_has_sent_analytics_events
expect([:web_request, :create_entity]).to have_been_enqueued_event_types
end
However, as we've both thought of it, maybe there's something to it. To make this work, I'd think we'd want:
expect_analytics_web_request_event_to_have_been_enqueued
expect_analytics_entity_event_to_have_been_enqueued
expect_any_analytics_event_to_have_been_enqueued
then perhaps for completeness:
have_sent_web_request_analytics_events
have_sent_entity_analytics_events
have_sent_any_analytics_events
and then maybe we could put these into `lib/dfe/analytics/rspec/matchers', or another helper file, to keep Rubocop from whinging.
Or we could just add documentation for the available event_types
and call it a day. 😉 What do you think?
Also change the behaviour have_sent_analytics_event_types so that it now has to match _all_ of the provided event_types matcher shortcuts. This means the request and entity shortcuts also have to go.
d6ee7d6
to
3192ff8
Compare
Ah, I didn't see
💯 good call! |
Just to mention, I've left the |
to have_been_enqueued_as_analytics_events
This matcher gets (optionally) installed as part of the generator.