瀏覽代碼

Iteration B2.

master
Nils Dittberner 9 年之前
父節點
當前提交
0afd0ce02b
共有 3 個檔案被更改,包括 79 行新增1 行删除
  1. +1
    -1
      app/models/product.rb
  2. +9
    -0
      test/fixtures/products.yml
  3. +69
    -0
      test/models/product_test.rb

+ 1
- 1
app/models/product.rb 查看文件

@@ -1,7 +1,7 @@
class Product < ApplicationRecord
validates :title, :description, :image_url, :price, presence: true
validates :price, numericality: {greater_than_or_equal_to: 0.01}
validates :title, uniqueness: true
validates :title, uniqueness: true, length: { minimum: 5, maximum: 30 }
validates :image_url, allow_blank: true, format: {
with: %r{\.(gif|jpg|png)\Z}i,
message: 'must be URL for GIF, JPG or PNG image.'


+ 9
- 0
test/fixtures/products.yml 查看文件

@@ -11,3 +11,12 @@ two:
description: MyText
image_url: MyString
price: 9.99

ruby:
title: Programming Ruby 1.9
description:
Ruby is the fastes growing and most exciting dynamic language out
there. If you need to get working programs delivered fast, you should
add Ruby to your toolbox.
price: 49.50
image_url: ruby.png

+ 69
- 0
test/models/product_test.rb 查看文件

@@ -1,7 +1,76 @@
require 'test_helper'

class ProductTest < ActiveSupport::TestCase
# Rails is already doing this! (convention over configuration!)
fixtures :products
# test "the truth" do
# assert true
# end

test "product attributes must not be empty" do
product = Product.new
assert product.invalid?
assert product.errors[:title].any?
assert product.errors[:description].any?
assert product.errors[:image_url].any?
assert product.errors[:price].any?
end

test "product price be positive" do
product = Product.new(title: 'My Book Title',
description: 'My Description.',
image_url: 'image.jpg')

product.price = -1
assert product.invalid?
assert_equal ['must be greater than or equal to 0.01'],
product.errors[:price]

product.price = 0
assert product.invalid?
assert_equal ['must be greater than or equal to 0.01'],
product.errors[:price]

product.price = 1
assert product.valid?
end

def new_product(image_url)
product = Product.new(title: 'My Book Title',
description: 'My Description.',
image_url: image_url,
price: 1)
end

test "image url" do
ok = %w{ fred.jpg hans.png paul.Png peter.JPG
http://a.b.c.d/images/foo.gif }
bad =%w{ fred.doc hans.gif/more }

ok.each { |name| assert new_product(name).valid? "#{name} shouldn't be invalid" }

bad.each do |name|
assert new_product(name).invalid? "#{name} shouldn't be valid"
end
end

test "product is not valid without a unique title - i18n" do
product = Product.new(title: products(:ruby).title,
description: 'Foo.',
price: 1,
image_url: "foo.jpg")
assert product.invalid?
assert_equal [I18n.translate('errors.messages.taken')],
product.errors[:title]
end

test "product is not valid without minimum 5 characters in title" do
product = Product.new(title: 'Shrt',
description: 'A short work.',
price: 0.99,
image_url: 'short.jpg')
assert product.invalid?
assert_equal ['is too short (minimum is 5 characters)'],
product.errors[:title]
end
end

Loading…
取消
儲存