Skip to content

Instantly share code, notes, and snippets.

@markahesketh
Created April 11, 2012 23:45
Show Gist options
  • Save markahesketh/2363522 to your computer and use it in GitHub Desktop.
Save markahesketh/2363522 to your computer and use it in GitHub Desktop.
# /app/models/product.rb
validates :title, :uniqueness => true,
:length => {:minimum => 10,
:message => 'Title is too short. Must be at least 10 characters'}
# /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?
assert_equal 'Title is too short. Must be at least 10 characters', product.errors[:title].join('; ')
product.title "And this"
assert product.invalid?
assert_equal 'Title is too short. Must be at least 10 characters', product.errors[:title].join('; ')
product.title "At least 10 characters long is correct"
assert product.valid?
end
--- OR ---
# /app/models/product.rb
validates :title, :uniqueness => true,
:length => {:minimum => 10,
:message => :too_short}
# /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?
assert_equal :too_short, product.errors[:title].join('; ')
product.title "And this"
assert product.invalid?
assert_equal :too_short, product.errors[:title].join('; ')
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