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

add have_sent_analytics_event rspec matcher #11

Merged
merged 8 commits into from
Jun 8, 2022

Conversation

misaka
Copy link
Contributor

@misaka misaka commented May 26, 2022

This matcher gets (optionally) installed as part of the generator.

@misaka misaka requested a review from duncanjbrown May 26, 2022 22:32
@misaka misaka changed the title add solargraph for greater Intellisense! add have_sent_analytics_event rspec matcher May 27, 2022
@misaka
Copy link
Contributor Author

misaka commented May 27, 2022

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.

@misaka misaka force-pushed the add-have_sent_analytics_event-spec-helper branch from 494ce5a to 4b03c48 Compare May 30, 2022 13:28
@misaka misaka force-pushed the add-have_sent_analytics_event-spec-helper branch 3 times, most recently from 75e6c3b to d6ee7d6 Compare June 7, 2022 15:00
@misaka
Copy link
Contributor Author

misaka commented Jun 8, 2022

Copy link
Contributor

@duncanjbrown duncanjbrown left a comment

Choose a reason for hiding this comment

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

Looking vg — nits only inline incl one about naming — wdyt?

Should RSpec be nested under Testing? DfE::Analytics::Testing::RSpec is verbose, but definitely not confusing.

What do you think of including matchers in our own test suite without the require_relative?

Screenshot 2022-06-08 at 10 47 40

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
Copy link
Contributor

Choose a reason for hiding this comment

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

Stray comment

# 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
Copy link
Contributor

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?

Copy link
Contributor Author

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?

lib/dfe/analytics/rspec/matchers/helpers.rb Outdated Show resolved Hide resolved
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
Copy link
Contributor

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?

Copy link
Contributor Author

@misaka misaka Jun 8, 2022

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
Copy link
Contributor

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...

Copy link
Contributor Author

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?

misaka added 4 commits June 8, 2022 11:20
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.
@misaka misaka force-pushed the add-have_sent_analytics_event-spec-helper branch from d6ee7d6 to 3192ff8 Compare June 8, 2022 10:25
@misaka
Copy link
Contributor Author

misaka commented Jun 8, 2022

Should RSpec be nested under Testing? DfE::Analytics::Testing::RSpec is verbose, but definitely not confusing.

Ah, I didn't see dfe/analytics/testing. Is the intention for these to be used in using projects? Or only in dfe-analytics? If the former then ya, let's keep them together, but also shouldn't we document those helpers? If the latter, then I wonder why those are in lib and not spec?

What do you think of including matchers in our own test suite without the require_relative?

💯 good call!

@misaka
Copy link
Contributor Author

misaka commented Jun 8, 2022

What do you think of including matchers in our own test suite without the require_relative?

💯 good call!

Just to mention, I've left the require in the spec files themselves, it seems potentially useful as part of the example of how to use the matchers, which I hope the specs provide.

@misaka misaka merged commit d10e3b8 into main Jun 8, 2022
@misaka misaka deleted the add-have_sent_analytics_event-spec-helper branch June 8, 2022 15:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants