Created
November 25, 2011 03:02
-
-
Save passingloop/1392736 to your computer and use it in GitHub Desktop.
ActiveRecord モデルテストの例
This file contains 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
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 |
This file contains 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
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 |
This file contains 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
class Foo < ActiveRecord::Base | |
validates :bar, presence: true, uniqueness: true | |
end |
This file contains 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
class Item < ActiveRecord::Base | |
belongs_to :vendor | |
validates :title, presence: true | |
validates :price, numericality: { :greater_than_or_equal_to => 0 } | |
end |
This file contains 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
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 |
This file contains 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
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ActiveRecord モデルテストの書き方ガイドライン に embed したもの