Rails测试错误---预期+++实际

时间:2016-12-18 16:23:58

标签: ruby-on-rails ruby unit-testing

以下是我收到的错误消息:

Failure:
ProductTest#test_product_price_must_be_positive [/home/jatin/work/depot/test/models/product_test.rb:18]:
--- expected
+++ actual
@@ -1 +1 @@
-["must be geater than or equal to 0.01"]
+["must be greater than or equal to 0.01"]

这是测试代码:

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?
assert_equal ["must be geater 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

这是app / models / product.rb文件中的代码:

class Product < ApplicationRecord
validates :title, :description, :image_url, presence: true
validates :price, numericality: {greater_than_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

请建议解决此问题的方法。

1 个答案:

答案 0 :(得分:1)

你有一个错字:

assert_equal ["must be geater than or equal to 0.01"], 
#                       ^ here
# Change to :
assert_equal ["must be greater than or equal to 0.01"],