Component | version |
---|---|
RubyMine | 5.0(Windows) |
Rails | 3.2.12 |
Rspec | 2.13.0 |
FactoryGirl | 4.2.0 |
Faker | 1.1.2 |
DatabaseCleaner | 0.9.1 |
1st Red
- テストを書く
spec/models/contact_spec.rb
it "has a valid factory" do
expect(FactoryGirl.build(:contact)).to be_valid
end
- Run > Run > spec
テストが走り、失敗する
Toggle auto-test
コードを書く
spec/factories/contacts.rb
FactoryGirl.define do
factory :contact do
firstname "Tatsuro"
lastname "Ueda"
sequence(:email) { |n| "weed#{n}@yahoo.co.jp"}
end
end
自動的にテストが走り、合格する
テストをFactoryGirl仕様に修正する
spec/models/contacts_spec.rb
it "is valid with a firstname and lastname" do
contact = FactoryGirl.build(
firstname: 'Tatsuro',
lastname: 'Tatsuro',
email: 'weed_7777@yahoo.co.jp'
)
expect(contact).to be_valid
end
it "is invalid without a firstname" do
#expect(Contact.new(firstname: nil)).to have(1).errors_on(:firstname)
contact = FactoryGirl.build(:contact, firstname: nil)
expect(contact).to have(1).errors_on(:firstname)
end
it "is invalid without a lastname" do
#expect(Contact.new(lastname: nil)).to have(1).errors_on(:lastname)
contact = FactoryGirl.build(:contact, lastname: nil)
expect(contact).to have(1).errors_on(:lastname)
end
it "is invalid with a duplicate email address" do
FactoryGirl.create(:contact, email: 'weed_7777@yahoo.co.jp')
contact = FactoryGirl.build(:contact, email: 'weed_7777@yahoo.co.jp')
expect(contact).to have(1).errors_on(:email)
end
describe "#name" do
it "returns a contact's full name as a string" do
contact = FactoryGirl.build(:contact,
firstname: "Tatsuro", lastname: "Ueda"
)
expect(contact.name).to eq 'Tatsuro Ueda'
end
end
自動的にテストが走り、合格する
「FactoryGirl」を省略可能にする
spec/spec_helper.rb
config.include FactoryGirl::Syntax::Methods
テストを修正する
テストをFactoryGirl仕様に修正する
spec/models/contacts_spec.rb
it "has a valid factory" do
expect(build(:contact)).to be_valid
end
it "is valid with a firstname and lastname" do
contact = build(:contact,
firstname: 'Tatsuro',
lastname: 'Tatsuro',
email: 'weed_7777@yahoo.co.jp'
)
expect(contact).to be_valid
end
it "is invalid without a firstname" do
contact = build(:contact, firstname: nil)
expect(contact).to have(1).errors_on(:firstname)
end
it "is invalid without a lastname" do
contact = build(:contact, lastname: nil)
expect(contact).to have(1).errors_on(:lastname)
end
it "is invalid with a duplicate email address" do
create(:contact, email: 'weed_7777@yahoo.co.jp')
contact = build(:contact, email: 'weed_7777@yahoo.co.jp')
expect(contact).to have(1).errors_on(:email)
end
describe "#name" do
it "returns a contact's full name as a string" do
contact = build(:contact,
firstname: "Tatsuro", lastname: "Ueda"
)
expect(contact.name).to eq 'Tatsuro Ueda'
end
end
- 自動的にテストが走り、合格する
2nd Red
- テストを書く
spec/models/phones_spec.rb
it "allows two contacts to share a phone number" do
#contact = Contact.create(
# firstname: 'Tatsuro', lastname: 'Ueda', email: 'weed_7777@yahoo.co.jp'
#)
#contact.phones.create(
# phone_type: 'home',
# phone: '01-2345-6789'
#)
#other_contact = Contact.new
#other_phone = other_contact.phones.build(
# phone_type: 'home',
# phone: '01-2345-6789'
#)
create(:phone, phone: "01-2345-6789")
other_phone = build(:phone, phone: "01-2345-6789")
expect(other_phone).to be_valid
end
自動的にテストが走り、コケる
コードを書く
spec/factories/phones.rb
FactoryGirl.define do
factory :phone do
association :contact
phone_type "home"
phone "01-2345-6789"
end
end
- 自動的にテストが走り、合格する
3rd Red
- テストを書く
spec/models/phones_spec.rb
create(:home_phone, phone: "01-2345-6789")
other_phone = build(:home_phone, phone: "01-2345-6789")
expect(other_phone).to be_valid
自動的にテストが走り、コケる
コードを書く
spec/factories/phones.rb
FactoryGirl.define do
factory :phone do
association :contact
phone "01-2345-6789"
factory :home_phone do
phone_type "home"
end
factory :work_phone do
phone_type "work"
end
factory :mobile_phone do
phone_type "mobile"
end
end
end
- 自動的にテストが走り、合格する
Faker導入
- コードを書く
spec/factories/contacts.rb
FactoryGirl.define do
factory :contact do
firstname { Faker::Name.first_name }
lastname { Faker::Name.last_name }
email { Faker::Internet.email }
end
end
spec/factories/phones.rb
FactoryGirl.define do
factory :phone do
association :contact
phone { Faker::PhoneNumber.phone_number }
...
- 自動的にテストが走り、合格する
4th Red
- テストを書く
spec/models/contacts_spec.rb
it "has three phone numbers" do
expect(create(:contact).phones.count).to eq 3
end
自動的にテストが走り、コケる
コードを書く
spec/factories/contacts.rb
FactoryGirl.define do
factory :contact do
firstname { Faker::Name.first_name }
lastname { Faker::Name.last_name }
email { Faker::Internet.email }
after(:build) do |contact|
[:home_phone, :work_phone, :mobile_phone].each do |phone|
contact.phones << FactoryGirl.build(:phone,
phone_type: phone, contact: contact)
end
end
end
end
- 自動的にテストが走り、合格する
ブログやってます:PAPA-tronix !