lundi 30 novembre 2015

Length Validation Testing Rails Test:Unit

I am just getting into Rails testing, and I am using the built in testing "language" as opposed to Rspec. For the life of me I can't figure out why this is still failing.

test "product title is at least 10 chars long" do
    product = Product.new(description: 'yyy',
                          image_url: 'zzz.jpg',
                          price: 1)
    product.title = 'testitout'
    assert product.invalid?
    assert_equal ['title must be at least 10 chars long'], product.errors[:title]

    product.title = 'testitoutt'
    assert product.valid?
end

And here is the Product.rb

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, allow_blank: true, format: {
    with: %r{\.(gif|jpg|png)\Z}i,
    message: 'must be a URL for GIF, JPG, or PNG'
  }
  validates :title, length: { minimum: 10 }
end

Aucun commentaire:

Enregistrer un commentaire