Rails Warns On Tests Without Assertions.

With Rails 7.2, the ActiveSupport::TestCase now emits warnings when tests are executed without any assertions.

This update ensures tests are actually verifying behavior, preventing the false security of tests that pass without checking anything.

It encourages meaningful tests, improves code quality, and maintains robust test coverage to reduce the risk of undetected bugs.

# app/models/user.rb

class User < ApplicationRecord
  validates :name, presence: true
end

Before

Prior to Rails 7.2, ActiveSupport::TestCase does not issue warnings on tests without assertions.

# test/models/user_test.rb

require "test_helper"

class UserTest < ActiveSupport::TestCase
  def test_user_creation
    user = User.new(name: "Alice")
  end
end

# Running:

Finished in 0.035947s, 27.8187 runs/s, 0.0000 assertions/s.
1 runs, 0 assertions, 0 failures, 0 errors, 0 skips

The above test passes without any assertions and it won’t raise any warnings.

After

Rails 7.2 now warns on tests without assertions.

# test/models/user_test.rb

require "test_helper"

class UserTest < ActiveSupport::TestCase
  def test_user_creation
    user = User.new(name: "Alice")
  end
end

# Running:

Test is missing assertions: `test_user_creation` /test/models/user_test.rb:4

Finished in 0.070524s, 14.1796 runs/s, 0.0000 assertions/s.
1 runs, 0 assertions, 0 failures, 0 errors, 0 skips

The above test passes without any assertions and but it raises warning Test is missing assertions.

This is helpful in detecting broken tests that do not perform intended assertions.

# test/models/user_test.rb

require "test_helper"

class UserTest < ActiveSupport::TestCase
  def test_user_creation
    user = User.new(name: "Alice")
    assert user.valid?, "User should be valid with a name"
  end
end

# Running:

Finished in 0.609933s, 1.6395 runs/s, 1.6395 assertions/s.
1 runs, 1 assertions, 0 failures, 0 errors, 0 skips

The above test passes with assertions and without any warnings.

Refer

Need help on your Ruby on Rails or React project?

Join Our Newsletter