Last active
August 29, 2015 14:13
-
-
Save yesnik/d59fe3f207a1caecb6ca to your computer and use it in GitHub Desktop.
Пример синтаксиса Ruby тестов
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# encoding: utf-8 | |
require 'test_helper' | |
class ProductTest < ActiveSupport::TestCase | |
# Определяем, какую из фикстур загружать для теста | |
# Это будет: test/fixtures/products.yml | |
# fixtures :products - Rails загрузит это по умолчанию | |
# Свойства товара не должны быть пустыми | |
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[:price].any? | |
assert product.errors[:image_url].any? | |
end | |
# Цена товара должна быть положительной | |
test 'product price must be positive' do | |
product = Product.new( | |
title: 'My Super Book', | |
description: 'Some book description', | |
image_url: 'MovieFilm-DVD.jpg' | |
) | |
product.price = -1 | |
assert product.invalid? | |
assert_equal "must be greater than or equal to 0.01", | |
product.errors[:price].join('; ') | |
product.price = 0 | |
assert product.invalid? | |
assert_equal "must be greater than or equal to 0.01", | |
product.errors[:price].join('; ') | |
product.price = 1 | |
assert product.valid? | |
end | |
# Вспомогательный метод для создания экземпляра продукта | |
def new_product(image_url) | |
Product.new( | |
title: 'My Sup Book', description: 'aaa', | |
price: 10.11, image_url: image_url) | |
end | |
# URL изображения должно иметь корректное расширение | |
test 'image_url' do | |
ok = %w{ fred.gif fred.jpg fred.png http://site.com/book.JPG aa.Png } | |
ok.each do |name| | |
assert new_product(name).valid?, "#{ name } - должен быть корректен" | |
end | |
bad = %w{ fred.doc fred.gif/more fred.gif.more } | |
bad.each do |name| | |
assert new_product(name).invalid?, "#{ name } - НЕ должен быть корректен" | |
end | |
end | |
# Имя товара должно быть уникальным | |
test 'product is not valid without a unique title - i18n' do | |
# Пробуем создать товар с существующим заголовком | |
product = Product.new( | |
title: products(:ruby_book).title, | |
description: 'bbb', | |
price: 2, | |
image_url: 'dolly.gif') | |
assert !product.save | |
assert_equal I18n.t('errors.messages.taken'), | |
product.errors[:title].join('; ') | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html | |
# File: test/fixtures/products.yml | |
# Это данные, которые будут загружены в тестовую БД при запуске `rake test:models` | |
ruby_book: | |
title: Programming Ruby 1.9 | |
description: | |
Ruby is the greate language for all | |
of us. Learn Ruby as fast as possible! | |
image_url: ruby.png | |
price: 51.29 | |
two: | |
title: MyString | |
description: MyText | |
image_url: MyString | |
price: 9.99 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment