Skip to content

Increment test assertions count#104

Merged
kou merged 1 commit intorr:masterfrom
erickguan:assert
Apr 7, 2025
Merged

Increment test assertions count#104
kou merged 1 commit intorr:masterfrom
erickguan:assert

Conversation

@erickguan
Copy link
Contributor

@erickguan erickguan commented Apr 4, 2025

This PR updates Minitest assertion counts when using assert_received, aligning with a recent update in Rails 7.2. From Rails 7.2, tests without assertions emit warnings like "Test is missing assertions". This update no longer triggers Rails' warnings.

@kou
Copy link
Member

kou commented Apr 5, 2025

Could you share warnings you saw?

It seems that the added test without your fix don't show any warnings.

@erickguan
Copy link
Contributor Author

Sorry, I was ahead of myself. The warning comes from Rails with Minitest.

I set up this Rails app with an index controller:

class IndexController < ApplicationController
  module Text
    def self.text
      "hello, world"
    end
  end

  def index
    render plain: Text.text
  end
end

class IndexControllerTest < ActionDispatch::IntegrationTest
  test "should get index" do
    stub(IndexController::Text).text { "stub" }

    get root_url
    assert_response 200
    assert_equal "stub", response.body
  end
end

Expected

Test works without warnings:

> rails test test/controllers/index_controller_test.rb
Running 1 tests in a single process (parallelization threshold is 50)
Run options: --seed 12192

# Running:

.

Finished in 0.043967s, 22.7443 runs/s, 45.4887 assertions/s.
1 runs, 2 assertions, 0 failures, 0 errors, 0 skips

When I use RR, that is using the assert_received without other assertions:

class IndexControllerTest < ActionDispatch::IntegrationTest
  test "should get index" do
    stub(IndexController::Text).text { "stub" }

    get root_url
    assert_received(IndexController::Text) do |expect|
      expect.text
    end
  end
end

I should get 1 runs, 1 assertions, and Rails should not warn me about missing assertions.

Actual

rails test test/controllers/index_controller_test.rb
Running 1 tests in a single process (parallelization threshold is 50)
Run options: --seed 22002

# Running:

Test is missing assertions: `test_should_get_index` testapp/test/controllers/index_controller_test.rb:4
.

Finished in 0.057891s, 17.2738 runs/s, 0.0000 assertions/s.
1 runs, 0 assertions, 0 failures, 0 errors, 0 skips

With this patch

rails test test/controllers/index_controller_test.rb
Running 1 tests in a single process (parallelization threshold is 50)
Run options: --seed 38290

# Running:

.

Finished in 0.060685s, 16.4785 runs/s, 16.4785 assertions/s.
1 runs, 1 assertions, 0 failures, 0 errors, 0 skips

@kou
Copy link
Member

kou commented Apr 7, 2025

Thanks. It seems that the following change is enough:

diff --git a/lib/rr/integrations/minitest_4.rb b/lib/rr/integrations/minitest_4.rb
index 8e73aa8..90bacfc 100644
--- a/lib/rr/integrations/minitest_4.rb
+++ b/lib/rr/integrations/minitest_4.rb
@@ -3,6 +3,7 @@ module RR
     class MiniTest4
       module Mixin
         def assert_received(subject, &block)
+          self.assertions += 1
           block.call(received(subject)).call
         end
       end

What do you think about this?

@erickguan
Copy link
Contributor Author

erickguan commented Apr 7, 2025

Of course. I will move the change to minitest4.


Updated. I kept the test case to test the assertions count on minitest5.

Copy link
Member

@kou kou left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you update the PR description to explain what warning was shown before we merge this?

end

result = test_class.new(:test_assert_received).run
assert_empty result.failures
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this? (Is it important in this test?)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, removed.


result = test_class.new(:test_assert_received).run
assert_empty result.failures
assert_equal 1, result.assertions
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
assert_equal 1, result.assertions
assert_equal(1, result.assertions)

@kou
Copy link
Member

kou commented Apr 7, 2025

(We'll use the PR description for commit message.)

@erickguan
Copy link
Contributor Author

Thanks for the context! Updated.

@erickguan
Copy link
Contributor Author

Would you want another PR to support open struct?

RR looks to be quite backward compatible to me. If you want to keep the backwards compatible promise, I suggest the 'defined?' check for now.

@kou kou merged commit ba83e89 into rr:master Apr 7, 2025
13 checks passed
@kou
Copy link
Member

kou commented Apr 7, 2025

Would you want another PR to support open struct?

I've already fixed it :-)
29ce3b2

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