-
-
Notifications
You must be signed in to change notification settings - Fork 1k
WIP: 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
35
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
35 commits
Select commit
Hold shift + click to select a range
9294186
first pass at have_reported_error matcher
skatkov 231ec91
Adding a feature
skatkov 040ea69
simplify matcher/readmer.md
skatkov 584a93b
Remove mocks out of tests
skatkov 0c2ce03
test failure messages better
skatkov 404f6bb
rewrite specs
skatkov d32437f
should require a block matcher
skatkov 9e938ce
Create ErrorEvent struct to store errors
skatkov bc9acfb
simplify matcher
skatkov f41ba1f
disable matcher chaining
skatkov 7de1c60
improved argument error message in case block is not passed to matcher
skatkov 32d35ef
further clean-up
skatkov 492c728
Apply suggestions from code review
skatkov c3e9343
clean-up features
skatkov 9907d1b
remove a require from test
skatkov f58e4a7
rename .with to .with_context
skatkov 834b3bc
have_reported_error to accept class name and a message
skatkov dad179b
switch to error_report matcher type of api
skatkov 1b81eac
refactor
skatkov a30c7ae
Using UndefinedValue as a default attribute
skatkov 07a63d8
Look through all errors, don't just go with last error
skatkov 9d87a40
warn about nil being passed
skatkov d620e9d
Use ErrorCollector from Rails instead of self-written version'
07a8868
Use defined UNDEFINED value instead of defining our own
ef38475
Update features/matchers/README.md
pirj 248e743
Define supports_value_expectations? to false
cf1def7
remove log/development.log
569fa0e
Don't use default values in HaveReportedError initializer
c09b42c
Don't allow to redefine attributes with chaining in with_context method
47840d1
adjust .feature file to properly illustrate report usage
b6d0b0d
handle hegated matchers safely, not allowing any attributes
091d8b8
Remove from features/matchers/README.md
skatkov ad78776
cleaning up a class
aa4c84d
remove context attribute in specs
b20a699
block chaining for .or method as well
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
Some comments aren't visible on the classic Files Changed page.
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"), severity: :error, context: { topic: "user_processing" }) | ||
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 | ||
skatkov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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.