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

Condense built-in and DSL RSpec matcher inspection output #203

Merged
merged 6 commits into from
Nov 14, 2023
Merged

Condense built-in and DSL RSpec matcher inspection output #203

merged 6 commits into from
Nov 14, 2023

Conversation

jas14
Copy link
Collaborator

@jas14 jas14 commented Nov 4, 2023

Problem

In diff output, some RSpec matchers, such as custom DSL-defined matchers and built-ins like be_a, are treated as default objects. This means that their fully-qualified class names and all instance variables are recursively printed, which can make the output quite verbose! Here's a very small example spec:

expected = hash_including(number: be_a(Numeric))
actual = {number: 4}
expect(actual).not_to match(expected)

which outputs the following diff:

                {
              -   number: #<RSpec::Matchers::BuiltIn::BeAKindOf:0x00000001062308f0 {
              -     @actual="not a number",
              -     @expected=Numeric
              -   }>
              +   number: "not a number"
                }

You can imagine this could get much longer with more complex expectations.

Solution

Use the matcher's description on inspection. For the case above, this results in the following:

                {
              -   number: (be a kind of Numeric)
              +   number: "not a number"
                }

Copy link
Collaborator

@mcmire mcmire left a comment

Choose a reason for hiding this comment

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

Hi @jas14! Thanks for the PR. Sorry for the delay in reviewing, I'm slowly getting back to this library. Just had a few comments.

spec/integration/rspec/unhandled_matcher_spec.rb Outdated Show resolved Hide resolved
spec/unit/rspec/object_inspection/rspec_matcher_spec.rb Outdated Show resolved Hide resolved
spec/unit/rspec/object_inspection/rspec_matcher_spec.rb Outdated Show resolved Hide resolved
jas14 and others added 2 commits November 13, 2023 17:41
@mcmire mcmire merged commit 805537f into splitwise:main Nov 14, 2023
38 checks passed
@mcmire
Copy link
Collaborator

mcmire commented Nov 14, 2023

Thanks so much!

@jas14 jas14 deleted the rspec-matcher-inspection branch November 15, 2023 01:17
@mcmire
Copy link
Collaborator

mcmire commented Feb 10, 2024

I updated the inspector you added to also consider custom-from-scratch matchers. This is included in v0.11.0. Thanks again!

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.

2 participants