我开始使用 Minitest 在 Rails 上进行单元测试。
这是我的第一个产品模型测试用例。
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 个断言 run。
$ 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 source , assert_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
关于ruby-on-rails - Minitest:Ruby on Rails 中的测试产生的断言比我预期的要多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32005317/