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/NoAssertions does not work with minitest/spec syntax #200

Closed
Narnach opened this issue Nov 28, 2022 · 3 comments · Fixed by #203
Closed

Minitest/NoAssertions does not work with minitest/spec syntax #200

Narnach opened this issue Nov 28, 2022 · 3 comments · Fixed by #203

Comments

@Narnach
Copy link

Narnach commented Nov 28, 2022

Hi,

For a project I recently joined I noticed there was a test without assert.

I added rubocop-minitest and enabled Minitest/NoAssertions to help detect this.

To my surprise Minitest/NoAssertions did not detect the test as having no assertions!

The root cause is that we use minitest/spec syntax rather than the class style. As a result we fail two internal preconditions of the current method used by NoAssertions to check if a method is a test and thus should be counted:

  • have a class name ending in "Test" (a top-level describe block does something different)
  • have a test name starting with "test_" (we use the it "does something" do .. end style instead of test "something" do .. end style)

I've created a failing test case similar to the ones that already exist in test/rubocop/cop/minitest/no_assertions_test.rb that describes the pattern of our test code. I'm unfamiliar with both Minitest and Rubocop internals so a fix appears to be harder to create.

Is this something that you would like to see resolved?

  def test_registers_offense_when_no_assertions_in_spec_form
    assert_offense(<<~RUBY)
      describe Foo do
        setup { }
        teardown { }

        it "asserts the truth" do
          assert true
        end

        it "does nothing" do
        ^^^^^^^^^^^^^^^^^^^^ Test case has no assertions.
        end

        describe "when seen from the other side" do
          it "refutes falsehood" do
            refute false
          end
        end
      end
    RUBY
  end
@Narnach Narnach changed the title Minitest/NoAssertions does not with minitest/spec syntax Minitest/NoAssertions does not work with minitest/spec syntax Nov 28, 2022
@andyw8
Copy link
Contributor

andyw8 commented Nov 29, 2022

It's not explicitly stated anywhere, but as far I can tell there's never been any attempt to support minitest/spec. Some cops may 'just work', but I suspect there are some more that don't, or that wouldn't make sense for minitest/spec.

koic added a commit to koic/rubocop-minitest that referenced this issue Dec 3, 2022
Fixes rubocop#200.

This PR supports `it` testing block for minitest/spec.

`Minitest/GlobalExpectations` is already supported,
so `it` testing block can also be supported by RuboCop Minitest.
@koic
Copy link
Member

koic commented Dec 3, 2022

For example, Minitest/GlobalExpectations is already supported, so it testing block can also be supported by RuboCop Minitest. I've opened #203.

koic added a commit to koic/rubocop-minitest that referenced this issue Dec 3, 2022
Fixes rubocop#200.

This PR supports `it` testing block for minitest/spec.

`Minitest/GlobalExpectations` is already supported,
so `it` testing block can also be supported by RuboCop Minitest.
@koic koic closed this as completed in #203 Dec 5, 2022
koic added a commit that referenced this issue Dec 5, 2022
[Fix #200] Support `it` testing block for minitest/spec
@Narnach
Copy link
Author

Narnach commented Dec 9, 2022

Thanks!

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.

3 participants