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

RSpec matchers #2424

Merged
merged 2 commits into from
Oct 10, 2024
Merged

RSpec matchers #2424

merged 2 commits into from
Oct 10, 2024

Conversation

solnic
Copy link
Collaborator

@solnic solnic commented Oct 9, 2024

This adds an RSpec matcher to ease writing specs for Sentry events:

# in your spec_helper.rb
require "sentry/rspec"

# matching an event with a specific message
Sentry.capture_message("Oops")

expect(sentry_events).to include_sentry_event("Oops")

# matching an event that captured an exception
Sentry.capture_exception(StandardError.new("Oops"))

expect(sentry_events).to include_sentry_event(exception: StandardError, message: "Oops")

# matching on tags and context
expect(sentry_events).to include_sentry_event(
  exception: StandardError,
  message: "Oops",
  context: {hello: "world"},
  tags: {foo: "bar"}
)

# the same using chaining
expect(sentry_events).to
  include_sentry_event(exception: StandardError, message: "Oops")
    .with_context(hello: "world")
    .with_tags(foo: "bar")
)

Output when failed

     Failure/Error: expect(sentry_events).to include_sentry_event("Foo", context: { "rails.error" => { some: "stuff" } })
     
       Failed to find event matching:
     
         message: Foo
         context: {"rails.error"=>{:some=>"stuff"}}
         tags: {}
     
     
     
       Captured events:
     
       {
         "message": "Ooops",
         "contexts": {
           "os": {
             "name": "Linux",
             "version": "#1 SMP Mon Aug 12 08:47:01 UTC 2024",
             "build": "6.10.4-linuxkit",
             "kernel_version": "#1 SMP Mon Aug 12 08:47:01 UTC 2024",
             "machine": "aarch64"
           },
           "runtime": {
             "name": "ruby",
             "version": "ruby 3.3.4 (2024-07-09 revision be1089c8ec) [aarch64-linux]"
           },
           "trace": {
             "trace_id": "e26063cd42b146988432d9efbb995ac0",
             "span_id": "00237ec5105f44cf",
             "parent_span_id": null
           }
         },
         "tags": {
         }
       }
     
       {
         "message": "",
         "contexts": {
           "os": {
             "name": "Linux",
             "version": "#1 SMP Mon Aug 12 08:47:01 UTC 2024",
             "build": "6.10.4-linuxkit",
             "kernel_version": "#1 SMP Mon Aug 12 08:47:01 UTC 2024",
             "machine": "aarch64"
           },
           "runtime": {
             "name": "ruby",
             "version": "ruby 3.3.4 (2024-07-09 revision be1089c8ec) [aarch64-linux]"
           },
           "trace": {
             "trace_id": "e26063cd42b146988432d9efbb995ac0",
             "span_id": "00237ec5105f44cf",
             "parent_span_id": null
           }
         },
         "tags": {
         },
         "exception": {
           "values": [
             {
               "type": "StandardError",
               "value": "Gaah! (StandardError)",
               "module": "",
               "thread_id": 5260,
               "stacktrace": null,
               "mechanism": {
                 "type": "generic",
                 "handled": true
               }
             }
           ]
         }
       }

Closes #2395

@solnic solnic linked an issue Oct 9, 2024 that may be closed by this pull request
Copy link

codecov bot commented Oct 9, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.66%. Comparing base (9d3a7be) to head (2bfc2f4).
Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff            @@
##           master    #2424    +/-   ##
========================================
  Coverage   98.66%   98.66%            
========================================
  Files         218      220     +2     
  Lines       14339    14450   +111     
========================================
+ Hits        14147    14257   +110     
- Misses        192      193     +1     
Components Coverage Δ
sentry-ruby 99.01% <100.00%> (+0.01%) ⬆️
sentry-rails 97.34% <ø> (ø)
sentry-sidekiq 97.07% <ø> (ø)
sentry-resque 96.79% <ø> (-0.33%) ⬇️
sentry-delayed_job 98.92% <ø> (ø)
sentry-opentelemetry 100.00% <ø> (ø)
Files with missing lines Coverage Δ
sentry-ruby/lib/sentry/rspec.rb 100.00% <100.00%> (ø)
sentry-ruby/spec/sentry/rspec/matchers_spec.rb 100.00% <100.00%> (ø)

... and 3 files with indirect coverage changes

@solnic
Copy link
Collaborator Author

solnic commented Oct 9, 2024

@johngallagher @benoittgt 👋🏻 here's a WIP for the matchers, getting diffs in the output felt too hard so I just added printing sentry events dumped to hashes including only the keys that the matcher is dealing with (message, exception, tags and contexts).

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
@solnic solnic force-pushed the solnic/2395-rspec-matchers branch from 106c315 to 2bfc2f4 Compare October 10, 2024 10:47
@solnic solnic marked this pull request as ready for review October 10, 2024 10:48
@solnic solnic requested review from sl0thentr0py and st0012 October 10, 2024 10:49
@benoittgt
Copy link

Thanks @solnic. Love it !

@sl0thentr0py sl0thentr0py merged commit 499cbac into master Oct 10, 2024
141 checks passed
@sl0thentr0py sl0thentr0py deleted the solnic/2395-rspec-matchers branch October 10, 2024 15:48
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.

RSpec matchers
3 participants