对于有效值,Rails测试失败

时间:2012-03-04 11:02:50

标签: ruby-on-rails ruby ruby-on-rails-3

我有以下模型Product并测试相同。当我尝试运行测试时,我在第22行的有效值assert product.valid?上断言失败。 你能说出原因吗?

代码:

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

并且

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

This is the error

1 个答案:

答案 0 :(得分:2)

根据模型声明中的验证,您的image_url无效。