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

Reconfigure RSpec/PredicateMatcher for better expectation failure messages #13001

Merged
merged 1 commit into from
Jun 19, 2024

Conversation

Earlopain
Copy link
Contributor

@Earlopain Earlopain commented Jun 17, 2024

This is something that has bothered me for a while. There is an rspec cop for this to enforce better failure messages. I changed the config an let RuboCop autocorrect.

Changed expectations would previously output messages like this:

     Failure/Error: expect(options_keys.include?(:aautocorrect)).to be(true)

       expected true
            got false

The new expectations print the actual vs expected value:

     Failure/Error: expect(options_keys).to include(:aautocorrect)

       expected [:autocorrect_all, :autocorrect] to include :aautocorrect
       Diff:
       @@ -1 +1 @@
       -[:aautocorrect]
       +[:autocorrect_all, :autocorrect]

The same improvement also applies to something like expect($stderr.string.include?("whatever).to be(true) or expect(foo.nil?).to be(true) (and probably more)


Before submitting the PR make sure the following are checked:

  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Ran bundle exec rake default. It executes all tests and runs RuboCop on its own code.
  • Added an entry (file) to the changelog folder named {change_type}_{change_description}.md if the new code introduces user-observable changes. See changelog entry format for details.

@Earlopain Earlopain force-pushed the reconfigure-predicate-matcher branch from 44dcdb9 to fe8a333 Compare June 17, 2024 14:46
@bbatsov
Copy link
Collaborator

bbatsov commented Jun 17, 2024

Nice change, I like it. The output is much better, plus it reads better IMO.

.rubocop.yml Show resolved Hide resolved
@Earlopain Earlopain force-pushed the reconfigure-predicate-matcher branch from fe8a333 to f6d41ee Compare June 17, 2024 14:56
@Earlopain
Copy link
Contributor Author

Had to change some application code here: https://github.com/rubocop/rubocop/pull/13001/files#diff-fd0332e5689d0ab642df39f706a915663f5d8764d4fa4442d36a291b16de70fb

For some reason the test for that would fail only with rspec 4 if this returns nil instead of false.

…essages

Changed expectations would previously output messages like this:
```
     Failure/Error: expect(options_keys.include?(:aautocorrect)).to be(true)

       expected true
            got false
```

The new expectations print the actual vs expected value:
```
     Failure/Error: expect(options_keys).to include(:aautocorrect)

       expected [:autocorrect_all, :autocorrect] to include :aautocorrect
       Diff:
       @@ -1 +1 @@
       -[:aautocorrect]
       +[:autocorrect_all, :autocorrect]
```

The same improvement also applies to something like `expect($stderr.string.include?("whatever)`.to be(true)`
or `expect(foo.nil?).to be(true)` (and probably more)
@Earlopain Earlopain force-pushed the reconfigure-predicate-matcher branch from f6d41ee to 388d8dd Compare June 17, 2024 15:04
@bbatsov bbatsov merged commit eeb6042 into rubocop:master Jun 19, 2024
22 checks passed
@Earlopain Earlopain deleted the reconfigure-predicate-matcher branch July 26, 2024 12:54
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