Skip to content

Instantly share code, notes, and snippets.

@passingloop
Created November 25, 2011 03:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save passingloop/1392736 to your computer and use it in GitHub Desktop.
Save passingloop/1392736 to your computer and use it in GitHub Desktop.
ActiveRecord モデルテストの例
class CreateFoos < ActiveRecord::Migration
def change
create_table :foos do |t|
t.string :bar, null: false
t.timestamps
end
add_index :foos, :bar, unique: true
end
end
class CreateItems < ActiveRecord::Migration
def change
create_table :items do |t|
t.integer :vendor_id, null: false
t.string :title, null: false
t.string :description
t.decimal :price, precision: 10, scale: 2, null: false
t.timestamps
end
execute %{
ALTER TABLE items ADD CONSTRAINT title_check CHECK (title <> '');
ALTER TABLE items ADD CONSTRAINT price_check CHECK (price >= 0);
ALTER TABLE items ADD FOREIGN KEY (vendor_id) REFERENCES vendors (id);
}
end
end
class Foo < ActiveRecord::Base
validates :bar, presence: true, uniqueness: true
end
class Item < ActiveRecord::Base
belongs_to :vendor
validates :title, presence: true
validates :price, numericality: { :greater_than_or_equal_to => 0 }
end
require 'test_helper'
class ItemTest < ActiveSupport::TestCase
test 'an item with boundary value attributes is valid' do
assert build(:item_with_boundary_value_attributes).valid?
end
test 'an item with empty title is invalid' do
item = build(:item_with_empty_title)
assert item.invalid?
assert item.errors[:title].any?
end
test 'an item with blank title is invalid' do
item = build(:item_with_blank_title)
assert item.invalid?
assert item.errors[:title].any?
end
test 'an item with empty price is invalid' do
item = build(:item_with_empty_price)
assert item.invalid?
assert item.errors[:price].any?
end
test 'an item with negative price is invalid' do
item = build(:item_with_negative_price)
assert item.invalid?
assert item.errors[:price].any?
end
test 'db allows saving an item with boundary value attributes' do
assert build(:item_with_boundary_value_attributes).save
end
test 'db can catch null title' do
assert_statement_invalid do
build(:item_with_empty_title).save!(validate: false)
end
end
test 'db can catch blank title' do
assert_statement_invalid do
build(:item_with_blank_title).save!(validate: false)
end
end
test 'db can catch null price' do
assert_statement_invalid do
build(:item_with_empty_price).save!(validate: false)
end
end
test 'db can catch negative price' do
assert_statement_invalid do
build(:item_with_negative_price).save!(validate: false)
end
end
test 'db can catch null vendor_id' do
assert_statement_invalid do
build(:item, vendor_id: nil).save!(validate: false)
end
end
test 'db can catch an item with invalid reference' do
assert_invalid_foreign_key do
build(:item, vendor_id: 99999999).save!(validate: false)
end
end
end
FactoryGirl.define do
factory :item, aliases: [:item_with_boundary_value_attributes] do
vendor
title "Q"
description "Using this item, you will be taller."
price '0.00'
factory :item_with_empty_title do
title nil
end
factory :item_with_blank_title do
title ''
end
factory :item_with_empty_price do
price nil
end
factory :item_with_negative_price do
price '-128.75'
end
end
end
@passingloop
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment