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/GlobalExpectations autocorrect fails with lambdas #142

Closed
Aqualon opened this issue Sep 21, 2021 · 1 comment · Fixed by #149
Closed

Minitest/GlobalExpectations autocorrect fails with lambdas #142

Aqualon opened this issue Sep 21, 2021 · 1 comment · Fixed by #149

Comments

@Aqualon
Copy link

Aqualon commented Sep 21, 2021

Expected behavior

Autocorrected test should run without the warning

Actual behavior

Test is failing

Steps to reproduce the problem

I inherited an internal gem, that has some minitests with the pattern, that a lambda is defined that has raising code and should verify the correct raise.

require "minitest/autorun"
require "mocha/minitest"

describe 'the test' do
  it 'raises' do
    expected = -> {
      raise ArgumentError.new("foo")
    }.must_raise ArgumentError

    expected.message.must_match(/foo/)
  end
end

This gives the expected warning while running

DEPRECATED: global use of must_raise from test.rb:6. Use _(obj).must_raise instead. This will fail in Minitest 6.
DEPRECATED: global use of must_match from test.rb:10. Use _(obj).must_match instead. This will fail in Minitest 6.

Running rubocop gives the warning

rubocop --require rubocop-minitest --only Minitest/GlobalExpectations test/test.rb 
Inspecting 1 file
C

Offenses:

test/test.rb:6:16: C: [Correctable] Minitest/GlobalExpectations: Use _ { -> {
      raise ArgumentError.new("foo")
    } } instead.
    expected = -> { ...
               ^^^^
test/test.rb:10:5: C: [Correctable] Minitest/GlobalExpectations: Use _(expected.message) instead.
    expected.message.must_match(/foo/)
    ^^^^^^^^^^^^^^^^

1 file inspected, 2 offenses detected, 2 offenses auto-correctable

Autocorrect creates the following test:

require "minitest/autorun"
require "mocha/minitest"

describe 'the test' do
  it 'raises' do
    expected = _ { -> {
      raise ArgumentError.new("foo")
    } }.must_raise ArgumentError

    _(expected.message).must_match(/foo/)
  end
end

And this fails with

  1) Failure:
the test#test_0001_raises [test/test.rb:6]:
ArgumentError expected but nothing was raised.

Correcting it to

require "minitest/autorun"
require "mocha/minitest"

describe 'the test' do
  it 'raises' do
    expected = _(-> {
      raise ArgumentError.new("foo")
    }).must_raise ArgumentError

    _(expected.message).must_match(/foo/)
  end
end

works and shows no longer the initial warning by minitest.

RuboCop version

❯ rubocop -V --require rubocop-minitest
1.20.0 (using Parser 3.0.2.0, rubocop-ast 1.11.0, running on ruby 2.7.4 x86_64-darwin20)
  - rubocop-minitest 0.15.0
@Aqualon
Copy link
Author

Aqualon commented Sep 21, 2021

I also did a full rubocop autocorrect, which created

require 'minitest/autorun'
require 'mocha/minitest'

describe 'the test' do
  it 'raises' do
    expected = _ do
      lambda {
        raise ArgumentError, 'foo'
      }
    end.must_raise ArgumentError

    _(expected.message).must_match(/foo/)
  end
end

also failing with

  1) Failure:
the test#test_0001_raises [test/test.rb:6]:
ArgumentError expected but nothing was raised.

gi added a commit to gi/rubocop-minitest that referenced this issue Oct 18, 2021
…ver is lambda

This fixes an issue when autocorrecting offenses where the receiver is
a lambda function: `-> { n }`.
The expected correct result is `_ { n }` instead of `_ { -> { n } }`.
gi added a commit to gi/rubocop-minitest that referenced this issue Oct 18, 2021
…ver is lambda

This fixes an issue when autocorrecting offenses where the receiver is
a lambda function: `-> { n }`.
The expected correct result is `_ { n }` instead of `_ { -> { n } }`.
gi added a commit to gi/rubocop-minitest that referenced this issue Oct 18, 2021
…ver is lambda

This fixes an issue when autocorrecting offenses where the receiver is
a lambda function: `-> { n }`.
The expected correct result is `_ { n }` instead of `_ { -> { n } }`.
gi added a commit to gi/rubocop-minitest that referenced this issue Oct 18, 2021
…ver is lambda

This fixes an issue when autocorrecting offenses where the receiver is
a lambda function: `-> { n }`.
The expected correct result is `_ { n }` instead of `_ { -> { n } }`.

Fixes rubocop#142
gi added a commit to gi/rubocop-minitest that referenced this issue Oct 19, 2021
…ver is lambda

This fixes an issue when autocorrecting offenses where the receiver is
a lambda function: `-> { n }`.
The expected correct result is `_ { n }` instead of `_ { -> { n } }`.

Fixes rubocop#142
@koic koic closed this as completed in #149 Oct 19, 2021
koic added a commit that referenced this issue Oct 19, 2021
…rect-lambda-receiver

[Fix #142] Minitest/GlobalExpectations: autocorrect when receiver is lambda
gi added a commit to gi/rubocop-minitest that referenced this issue Oct 19, 2021
…nfig parameter

Add a new configuration value for Minitest/GlobalExpectations: EnforcedStyle.
This value sets the allowed and preferred method(s) with which to detect and
correct errors.

Fixes rubocop#142
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 a pull request may close this issue.

1 participant