在尝试为模型编写单元测试时,我不断收到相同的错误并且似乎无法修复它。
这是我的测试:
require 'test_helper'
class ProductTest < ActiveSupport::TestCase
test "product attirbutes must not be empty" do
product = Product.new
assert product.invalid?
assert product.errors[:title].any?
assert product.errors[:description].any?
assert product.errors[:price].any?
assert product.errors[:image_url].any?
end
test "product price must be positive" do
product = Product.new(title: "My Book Title",
description: "yyy",
image_url: "zzz.jpg")
product.price = -1
# line number 19 below
assert product.invalid?
assert_equal ["must be greater than or equal to 0.01"],
product.errors[:price]
product.price = 0
assert product.invalid?
assert_equal ["must be greater than or equal to 0.01"],
product.errors[:price]
product.price = 1
assert product.valid?
end
end
当我运行 > rake test
我收到以下错误:
1) Failure:
ProductTest#test_product_price_must_be_positive
/test/models/product_test.rb:19]:
Failed assertion, no message given.
这是我的模型:
class Product < ActiveRecord::Base
validates :title, :description, :image_url, presence: true
validates :price, numericality: {greater_then_or_equal_to: 0.01}
validates :title, uniqueness: true
validates :image_url, allow_blank: true, format: {
with: %r{\.(gif|jpg|png)\Z}i,
message: 'must be a url for GIF, JPG or PNG image.'
}
end
我不知道这里发生了什么,请帮忙!