When trying to write a unit test for models I keep getting the same error and cant seem to fix it.
This is my test:
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
When I run > rake test
I get the following error:
1) Failure:
ProductTest#test_product_price_must_be_positive
/test/models/product_test.rb:19]:
Failed assertion, no message given.
Here is my model:
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
I have no idea what is going on here please help!