Skip to content

Instantly share code, notes, and snippets.

@markahesketh
Created April 12, 2012 13:02
Show Gist options
  • Save markahesketh/2367063 to your computer and use it in GitHub Desktop.
Save markahesketh/2367063 to your computer and use it in GitHub Desktop.
# /app/models/product.rb
validates :title, :description, :image_url, :presence => true
validates :price, :numericality => {:greater_than_or_equal_to => 0.01}
validates :title, :uniqueness => true,
:length => {:minimum => minimum = 10,
:message => "is too short. Must be at least #{minimum} characters."}
validates :image_url, :format => {:with => %r{\.(gif|jpg|png)$}i,
:message => 'must be a URL for GIF, JPG, or PNG image.'
}
# /test/unit/product_test.rb
test "product title must be at least 10 characters long" do
product = Product.new(:description => "yyy",
:price => 1,
:image_url => "fred.gif")
product.title = "Incorrect"
assert product.invalid?, '1 - Title length is wrong'
product.title = "And this"
assert product.invalid?, '2 - Title length is wrong'
product.title = 'At least 10 characters long is correct'
assert product.valid?
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment