These two matchers are referenced from the blog post Segment.io and Ruby.
Last active
October 25, 2023 18:28
-
-
Save joshuaclayton/79987a3273ff125069dd to your computer and use it in GitHub Desktop.
RSpec custom matchers for Segment.io tracking
This file contains hidden or 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
# spec/support/analytics.rb | |
RSpec::Matchers.define :have_tracked do |event_name| | |
match do |backend| | |
@event_name = event_name | |
@backend = backend | |
backend.tracked_events_for(@user).named(@event_name).has_properties?(@properties) | |
end | |
description do | |
"tracked event" | |
end | |
failure_message_for_should do |text| | |
"expected event '#{@event_name}' to be tracked for user '#{@user}' with included properties #{@properties} but was not" | |
end | |
failure_message_for_should_not do |text| | |
"expected event '#{@event_name}' not to be tracked for user '#{@user}' with included properties #{@properties} but was" | |
end | |
chain(:for_user) { |user| @user = user } | |
chain(:with_properties) { |properties| @properties = properties } | |
end | |
RSpec::Matchers.define :have_identified do |user| | |
match do |backend| | |
@user = user | |
@backend = backend | |
@traits ||= {} | |
backend.has_identified?(@user, @traits) | |
end | |
description do | |
"identified user" | |
end | |
failure_message_for_should do |text| | |
"expected identification of user '#{@user}' with traits #{@traits}, who was not" | |
end | |
failure_message_for_should_not do |text| | |
"expected no identification of user '#{@user}' with traits #{@traits}, who was" | |
end | |
chain(:with_traits) { |traits| @traits = traits } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment