Created
December 8, 2011 03:30
-
-
Save passingloop/1445985 to your computer and use it in GitHub Desktop.
Shoulda で Rails テストにコンテキストを導入してみた例
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 | |
context 'with boundary value attributes' do | |
test 'an item with boundary value attributes is valid' do | |
assert build(:item_with_boundary_value_attributes).valid? | |
end | |
test 'db allows saving an item with boundary value attributes' do | |
assert build(:item_with_boundary_value_attributes).save | |
end | |
end | |
context 'with empty title' do | |
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 'db can catch null title' do | |
assert_statement_invalid do | |
build(:item_with_empty_title).save!(validate: false) | |
end | |
end | |
end | |
context 'with blank title' do | |
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 'db can catch blank title' do | |
assert_statement_invalid do | |
build(:item_with_blank_title).save!(validate: false) | |
end | |
end | |
end | |
context 'with empty price' do | |
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 'db can catch null price' do | |
assert_statement_invalid do | |
build(:item_with_empty_price).save!(validate: false) | |
end | |
end | |
end | |
context 'with negative price' do | |
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 can catch negative price' do | |
assert_statement_invalid do | |
build(:item_with_negative_price).save!(validate: false) | |
end | |
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
require 'test_helper' | |
class ItemTest < ActiveSupport::TestCase | |
context 'with boundary value attributes' do | |
should 'an item with boundary value attributes is valid' do | |
assert build(:item_with_boundary_value_attributes).valid? | |
end | |
should 'db allows saving an item with boundary value attributes' do | |
assert build(:item_with_boundary_value_attributes).save | |
end | |
end | |
context 'with empty title' do | |
should 'an item with empty title is invalid' do | |
item = build(:item_with_empty_title) | |
assert item.invalid? | |
assert item.errors[:title].any? | |
end | |
should 'db can catch null title' do | |
assert_statement_invalid do | |
build(:item_with_empty_title).save!(validate: false) | |
end | |
end | |
end | |
context 'with blank title' do | |
should 'an item with blank title is invalid' do | |
item = build(:item_with_blank_title) | |
assert item.invalid? | |
assert item.errors[:title].any? | |
end | |
should 'db can catch blank title' do | |
assert_statement_invalid do | |
build(:item_with_blank_title).save!(validate: false) | |
end | |
end | |
end | |
context 'with empty price' do | |
should 'an item with empty price is invalid' do | |
item = build(:item_with_empty_price) | |
assert item.invalid? | |
assert item.errors[:price].any? | |
end | |
should 'db can catch null price' do | |
assert_statement_invalid do | |
build(:item_with_empty_price).save!(validate: false) | |
end | |
end | |
end | |
context 'with negative price' do | |
should 'an item with negative price is invalid' do | |
item = build(:item_with_negative_price) | |
assert item.invalid? | |
assert item.errors[:price].any? | |
end | |
should 'db can catch negative price' do | |
assert_statement_invalid do | |
build(:item_with_negative_price).save!(validate: false) | |
end | |
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
require 'test_helper' | |
class ItemTest < ActiveSupport::TestCase | |
context 'an item with boundary value attributes' do | |
should 'be valid' do | |
assert build(:item_with_boundary_value_attributes).valid? | |
end | |
should 'be accepted by db' do | |
assert build(:item_with_boundary_value_attributes).save | |
end | |
end | |
context 'an item with empty title' do | |
should 'be invalid' do | |
item = build(:item_with_empty_title) | |
assert item.invalid? | |
assert item.errors[:title].any? | |
end | |
should 'be rejected by db' do | |
assert_statement_invalid do | |
build(:item_with_empty_title).save!(validate: false) | |
end | |
end | |
end | |
context 'an item with blank title' do | |
should 'an item with blank title is invalid' do | |
item = build(:item_with_blank_title) | |
assert item.invalid? | |
assert item.errors[:title].any? | |
end | |
should 'be rejected by db' do | |
assert_statement_invalid do | |
build(:item_with_blank_title).save!(validate: false) | |
end | |
end | |
end | |
context 'an item with empty price' do | |
should 'be invalid' do | |
item = build(:item_with_empty_price) | |
assert item.invalid? | |
assert item.errors[:price].any? | |
end | |
should 'be rejected by db' do | |
assert_statement_invalid do | |
build(:item_with_empty_price).save!(validate: false) | |
end | |
end | |
end | |
context 'an item with negative price' do | |
should 'be invalid' do | |
item = build(:item_with_negative_price) | |
assert item.invalid? | |
assert item.errors[:price].any? | |
end | |
should 'be rejected by db' do | |
assert_statement_invalid do | |
build(:item_with_negative_price).save!(validate: false) | |
end | |
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
require 'test_helper' | |
class ItemTest < ActiveSupport::TestCase | |
context 'an item with boundary value attributes' do | |
setup do | |
@item = build(:item_with_boundary_value_attributes) | |
end | |
should 'be valid' do | |
assert @item.valid? | |
end | |
should 'be accepted by db' do | |
assert @item.save | |
end | |
end | |
context 'an item with empty title' do | |
setup do | |
@item = build(:item_with_empty_title) | |
end | |
should 'be invalid' do | |
assert @item.invalid? | |
assert @item.errors[:title].any? | |
end | |
should 'be rejected by db' do | |
assert_statement_invalid do | |
@item.save!(validate: false) | |
end | |
end | |
end | |
context 'an item with blank title' do | |
setup do | |
@item = build(:item_with_blank_title) | |
end | |
should 'be invalid' do | |
assert @item.invalid? | |
assert @item.errors[:title].any? | |
end | |
should 'be rejected by db' do | |
assert_statement_invalid do | |
@item.save!(validate: false) | |
end | |
end | |
end | |
context 'an item with empty price' do | |
setup do | |
@item = build(:item_with_empty_price) | |
end | |
should 'be invalid' do | |
assert @item.invalid? | |
assert @item.errors[:price].any? | |
end | |
should 'be rejected by db' do | |
assert_statement_invalid do | |
@item.save!(validate: false) | |
end | |
end | |
end | |
context 'an item with negative price' do | |
setup do | |
@item = build(:item_with_negative_price) | |
end | |
should 'be invalid' do | |
assert @item.invalid? | |
assert @item.errors[:price].any? | |
end | |
should 'be rejected by db' do | |
assert_statement_invalid do | |
@item.save!(validate: false) | |
end | |
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
require 'test_helper' | |
class ItemTest < ActiveSupport::TestCase | |
context 'an item with boundary value attributes' do | |
setup do | |
@item = build(:item_with_boundary_value_attributes) | |
end | |
should 'be valid' do | |
assert @item.valid? | |
end | |
should 'be accepted by db' do | |
assert @item.save | |
end | |
end | |
[:item_with_blank_title, :item_with_empty_title, | |
:item_with_empty_price, :item_with_negative_price].each do |factory| | |
context "an #{factory}" do | |
setup do | |
@item = build(factory) | |
end | |
should 'be invalid' do | |
assert @item.invalid? | |
assert @item.errors[factory.to_s.split('_').last].any? | |
end | |
should 'be rejected by db' do | |
assert_statement_invalid do | |
@item.save!(validate: false) | |
end | |
end | |
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
本文はこちら → Shoulda で Rails のテストにコンテキストを導入する