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

Minitest/AssertWithExpectedArgument flags a common assertion in model tests. #126

Closed
ccasabona opened this issue Mar 21, 2021 · 2 comments · Fixed by #127
Closed

Minitest/AssertWithExpectedArgument flags a common assertion in model tests. #126

ccasabona opened this issue Mar 21, 2021 · 2 comments · Fixed by #127
Labels
bug Something isn't working

Comments

@ccasabona
Copy link

The new Minitest/AssertWithExpectedArgument cop flags a valid assertion used in Rails model tests.

With a User class that has a required name attribute, a test for this may be written as:

    test 'name exists' do
      @user.name = ''
      assert @user.invalid?, @user.errors[:name]
      assert_equal [I18n.t('errors.messages.blank')], @user.errors[:name]
      @user.name = 'charlie'
      assert @user.valid?, @user.errors.full_messages
    end

The last assertion, assert @user.valid?, @user.errors.full_messages, is (or used to be) an acceptable way of returning an unexpected error message for an assert something.valid? statement that you expect to be true. Should the @user instance not be valid, any errors are displayed.

Obviously, once the unexpected condition is corrected, the @user.error.full_messages part of the statement is no longer needed. Our model tests, however, have about 1300 lines that use this structure.

@koic koic added the bug Something isn't working label Mar 22, 2021
koic added a commit to koic/rubocop-minitest that referenced this issue Mar 22, 2021
Fixes rubocop#126.

This PR marks `Minitest/AssertWithExpectedArgument` as unsafe.

This cop is marked as unsafe, because it is not possible to determine
whether the second argument of `assert` is a message or not.
koic added a commit to koic/rubocop-minitest that referenced this issue Mar 22, 2021
Fixes rubocop#126.

This PR marks `Minitest/AssertWithExpectedArgument` as unsafe.

This cop is marked as unsafe, because it is not possible to determine
whether the second argument of `assert` is a message or not.
@koic koic closed this as completed in #127 Mar 30, 2021
koic added a commit that referenced this issue Mar 30, 2021
…as_unsafe

[Fix #126] Mark `Minitest/AssertWithExpectedArgument` as unsafe
@koic
Copy link
Member

koic commented Mar 30, 2021

RuboCop Minitest 0.11.1 has been released with unsafe mark to the cop. In the future, if there are many similar issues, I think this cop can consider disabling it by default. Thank you for your feedback.

@ccasabona
Copy link
Author

ccasabona commented Mar 30, 2021 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants