Reputation: 1949
I have following model Product
and test for the same. When I try to run the test, I get failure for assertion on valid value, assert product.valid?
, on line number 22.
Could you please tell why ?
Code :
require 'test_helper'
class ProductTest < ActiveSupport::TestCase
test "product must not accept with empty attributes" do
product = Product.new
assert product.invalid?
assert product.errors[:title].any?
assert product.errors[:description].any?
assert product.errors[:price].any?
end
test "product must not acept invalid amount values" do
product = Product.new(:title => "test title",
:description => "test description",
:image_url => "image_url"
)
product.price = -1
assert product.invalid?
product.price = 0
assert product.invalid?
product.price = 1
# Line number 22 below
assert product.valid?
end
end
And
class Product < ActiveRecord::Base
validates :title, :description, :image_url, :presence => true
validates :price, :numericality => {:greater_than_or_equal_to => 0.01}
validates :title, :uniqueness => true
validates :image_url, :format => {
:with => %r{\.(gif|png|jpg)$}i,
:message => 'must be a URL for image'
}
end
Upvotes: 0
Views: 120
Reputation: 726
Your image_url is not valid according to the validations in your Model declaration.
Upvotes: 2