-
-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Accessing an unregistered cop raises useful error
Currently, when a test class includes `AssertOffense`, `AssertOffense#setup` attempts to dynamically identify the Cop under test. If the Cop class cannot be found, `#setup` exits early. In these cases, the test cases continue to execute with a nil `@cop` since it wasn't set in `#setup` as expected. When helpers such as `#assert_offense` are used, they access `@cop` and the tests can fail with cryptic messages such as: ``` NoMethodError: undefined method `[]=' for nil ``` Initialization of the Cop was refactored to a lazily-called accessor method which has the same caching functionality via `@cop`, but raises an error if the expected Cop class isn't defined.
- Loading branch information
Showing
4 changed files
with
35 additions
and
10 deletions.
There are no files selected for viewing
1 change: 1 addition & 0 deletions
1
changelog/change_assert_offense_setup_fails_with_useful_message.md
This file contains 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 @@ | ||
* [#314](https://github.com/rubocop/rubocop-minitest/pull/314): **(Breaking)** Raise a useful error when using a Cop in `AssertOffense` if the Cop's class is not defined. ([@brandoncc][]) |
This file contains 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 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,22 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative '../../../test_helper' | ||
|
||
class AssertOffenseTest | ||
class CopNotDefinedTest < Minitest::Test | ||
def test_correct_failure_is_raised_when_cop_is_not_defined | ||
error = assert_raises RuntimeError do | ||
assert_offense(<<~RUBY) | ||
class FooTest < Minitest::Test | ||
def test_do_something | ||
assert_equal(nil, somestuff) | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `assert_nil(somestuff)`. | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
assert_includes error.message, 'Cop not defined' | ||
end | ||
end | ||
end |
This file contains 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