Minitest:Test in Ruby on Rails 产生的断言比我预期的要多

Minitest:Test in Ruby on Rails produces more assertions than I expect

我开始使用 Minitest 在 rails 上进行单元测试。

这是我对 Product 模型的第一个测试用例。

require "test_helper"

class ProductTest < ActiveSupport::TestCase
  test "product price must be positive" do
    product = Product.new(title: "My Book Title",
                          description: "yyy",
                          image_url: "zzz.jpg")
    product.price = -1
    assert product.invalid?, "negative price of a product must be invalid."
    assert product.errors.has_key?(:price), "an invalid product must have an error"

    product.price = 0
    assert product.invalid?, "negative price of a product must be invalid."
    assert product.errors.has_key?(:price), "an invalid product must have an error"

    product.price = 1
    assert product.valid?, "positive price of a product must be valid."
    assert_empty product.errors, "a valid product must have no error"
  end
end

我已经为这个测试用例添加了 6 个断言。 但是,如果我 运行 这个测试用例,控制台输出会产生 7 个断言 运行.

$ rake test test/models/product_test.rb 


[DEPRECATION] Ahoy subscribers are deprecated
Run options: --seed 31334

# Running:

.

Finished in 0.260717s, 3.8356 runs/s, 23.0134 assertions/s.

1 runs, 7 assertions, 0 failures, 0 errors, 0 skips

有没有人可以帮我弄清楚断言数量不匹配的原因

提前致谢。

查看the sourceassert_empty确实以一个的价格提出了两个断言。

def assert_empty obj, msg = nil
  msg = message(msg) { "Expected #{mu_pp(obj)} to be empty" }
  assert_respond_to obj, :empty?
  assert obj.empty?, msg
end