-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Introduce have_reported_error matcher #2849
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
Open
skatkov
wants to merge
21
commits into
rspec:main
Choose a base branch
from
skatkov:have-reported-error
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
5e6efcc
first pass at have_reported_error matcher
skatkov 5dd2c5c
Adding a feature
skatkov ce1a162
simplify matcher/readmer.md
skatkov 550c25e
Remove mocks out of tests
skatkov e074991
test failure messages better
skatkov 1fe7e91
rewrite specs
skatkov 6b41d2b
should require a block matcher
skatkov 3859b04
Create ErrorEvent struct to store errors
skatkov 8352668
simplify matcher
skatkov 030c27a
disable matcher chaining
skatkov 2b9bd02
improved argument error message in case block is not passed to matcher
skatkov 6135d5d
further clean-up
skatkov 8335664
Apply suggestions from code review
skatkov a14f5c7
clean-up features
skatkov af58d83
remove a require from test
skatkov 7259a25
rename .with to .with_context
skatkov 1b6deb6
have_reported_error to accept class name and a message
skatkov c77fca5
switch to error_report matcher type of api
skatkov f5e427a
refactor
skatkov 7b3abc5
Using UndefinedValue as a default attribute
skatkov 2488a71
Look through all errors, don't just go with last error
skatkov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,199 @@ | ||
Feature: `have_reported_error` matcher | ||
|
||
The `have_reported_error` matcher is used to check if an error was reported | ||
to Rails error reporting system (`Rails.error`). It can match against error | ||
classes, messages, and attributes. | ||
|
||
The matcher supports several matching strategies: | ||
* Any error reported | ||
* A specific error class | ||
* A specific error class with message | ||
* Error message patterns using regular expressions | ||
* Message-only matching (any class) | ||
* Error attributes using `.with_context()` | ||
|
||
The matcher is available in all spec types where Rails error reporting is used. | ||
|
||
Background: | ||
Given a file named "app/models/user.rb" with: | ||
"""ruby | ||
class User < ApplicationRecord | ||
skatkov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
class ValidationError < StandardError; end | ||
def self.process_data | ||
Rails.error.report(StandardError.new("Processing failed")) | ||
end | ||
|
||
def self.process_with_context | ||
Rails.error.report(ArgumentError.new("Invalid input"), context: { context: "user_processing", severity: :error }) | ||
end | ||
|
||
def self.process_custom_error | ||
Rails.error.report(ValidationError.new("Email is invalid")) | ||
end | ||
end | ||
""" | ||
|
||
Scenario: Checking for any error being reported | ||
Given a file named "spec/models/user_spec.rb" with: | ||
"""ruby | ||
require "rails_helper" | ||
|
||
RSpec.describe User do | ||
it "reports errors" do | ||
expect { | ||
User.process_data | ||
}.to have_reported_error | ||
end | ||
end | ||
""" | ||
When I run `rspec spec/models/user_spec.rb` | ||
Then the examples should all pass | ||
|
||
Scenario: Checking for message-only matching | ||
Given a file named "spec/models/user_spec.rb" with: | ||
"""ruby | ||
require "rails_helper" | ||
|
||
RSpec.describe User do | ||
it "reports error with exact message (any class)" do | ||
expect { | ||
User.process_data | ||
}.to have_reported_error("Processing failed") | ||
end | ||
|
||
it "reports error with message pattern (any class)" do | ||
expect { | ||
User.process_custom_error | ||
}.to have_reported_error(/Email/) | ||
end | ||
end | ||
""" | ||
When I run `rspec spec/models/user_spec.rb` | ||
Then the examples should all pass | ||
|
||
Scenario: Checking for a specific error class | ||
Given a file named "spec/models/user_spec.rb" with: | ||
"""ruby | ||
require "rails_helper" | ||
|
||
RSpec.describe User do | ||
it "reports a StandardError" do | ||
expect { | ||
User.process_data | ||
}.to have_reported_error(StandardError) | ||
end | ||
|
||
it "reports an ArgumentError" do | ||
expect { | ||
User.process_with_context | ||
}.to have_reported_error(ArgumentError) | ||
end | ||
end | ||
""" | ||
When I run `rspec spec/models/user_spec.rb` | ||
Then the examples should all pass | ||
|
||
Scenario: Checking for specific error class with message | ||
Given a file named "spec/models/user_spec.rb" with: | ||
"""ruby | ||
require "rails_helper" | ||
|
||
RSpec.describe User do | ||
it "reports error with specific message" do | ||
expect { | ||
User.process_data | ||
}.to have_reported_error(StandardError, "Processing failed") | ||
end | ||
|
||
it "reports ArgumentError with specific message" do | ||
expect { | ||
User.process_with_context | ||
}.to have_reported_error(ArgumentError, "Invalid input") | ||
end | ||
end | ||
""" | ||
When I run `rspec spec/models/user_spec.rb` | ||
Then the examples should all pass | ||
|
||
Scenario: Checking error messages using regular expressions | ||
Given a file named "spec/models/user_spec.rb" with: | ||
"""ruby | ||
require "rails_helper" | ||
|
||
RSpec.describe User do | ||
it "reports errors with a message matching a pattern (any class)" do | ||
expect { | ||
User.process_data | ||
}.to have_reported_error(/Processing/) | ||
end | ||
|
||
it "reports specific class with message matching a pattern" do | ||
expect { | ||
User.process_data | ||
}.to have_reported_error(StandardError, /Processing/) | ||
end | ||
end | ||
""" | ||
When I run `rspec spec/models/user_spec.rb` | ||
Then the examples should all pass | ||
|
||
Scenario: Constraining error matches to their attributes using `with_context` | ||
Given a file named "spec/models/user_spec.rb" with: | ||
"""ruby | ||
require "rails_helper" | ||
|
||
RSpec.describe User do | ||
it "reports error with specific context" do | ||
expect { | ||
User.process_with_context | ||
}.to have_reported_error.with_context(context: "user_processing") | ||
end | ||
|
||
it "reports error with multiple attributes" do | ||
expect { | ||
User.process_with_context | ||
}.to have_reported_error(ArgumentError).with_context(context: "user_processing", severity: :error) | ||
end | ||
end | ||
""" | ||
When I run `rspec spec/models/user_spec.rb` | ||
Then the examples should all pass | ||
|
||
Scenario: Checking custom error classes | ||
Given a file named "spec/models/user_spec.rb" with: | ||
"""ruby | ||
require "rails_helper" | ||
|
||
RSpec.describe User do | ||
it "reports a ValidationError" do | ||
expect { | ||
User.process_custom_error | ||
}.to have_reported_error(User::ValidationError) | ||
end | ||
|
||
it "reports ValidationError with specific message" do | ||
expect { | ||
User.process_custom_error | ||
}.to have_reported_error(User::ValidationError, "Email is invalid") | ||
end | ||
end | ||
""" | ||
When I run `rspec spec/models/user_spec.rb` | ||
Then the examples should all pass | ||
|
||
Scenario: Using negation - expecting no errors | ||
Given a file named "spec/models/user_spec.rb" with: | ||
"""ruby | ||
require "rails_helper" | ||
|
||
RSpec.describe User do | ||
it "does not report any errors for safe operations" do | ||
expect { | ||
# Safe operation that doesn't report errors | ||
"safe code" | ||
}.not_to have_reported_error | ||
end | ||
end | ||
""" | ||
When I run `rspec spec/models/user_spec.rb` | ||
Then the examples should all pass |
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.