Skip to content
This repository was archived by the owner on Aug 3, 2021. It is now read-only.

fin ch 4 #70

Open
wants to merge 1 commit into
base: 03_models
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/models/contact.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class Contact < ActiveRecord::Base
validates :firstname, presence: true
validates :lastname, presence: true
validates :email, presence: true, uniqueness: true
validates :phones, length: { is: 3 }

def name
[firstname, lastname].join(' ')
Expand Down
14 changes: 14 additions & 0 deletions spec/factories/contacts.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
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
19 changes: 19 additions & 0 deletions spec/factories/phones.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
FactoryGirl.define do
factory :phone do
association :contact
phone '123-555-1234'

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

63 changes: 34 additions & 29 deletions spec/models/contact_spec.rb
Original file line number Diff line number Diff line change
@@ -1,67 +1,72 @@
require 'rails_helper'

describe Contact do
it "is valid with a firstname, lastname and email" do
contact = Contact.new(
firstname: 'Aaron',
lastname: 'Sumner',
email: '[email protected]')
expect(contact).to be_valid

# checks that the factory is consistent with the model
it "has a valid factory" do
expect(build(:contact)).to be_valid
end



# it "is valid with a firstname, lastname and email" do
# contact = Contact.new(
# firstname: 'Aaron',
# lastname: 'Sumner',
# email: '[email protected]')
# expect(contact).to be_valid
# end

it "is invalid without a firstname" do
contact = Contact.new(firstname: nil)
contact = build(:contact, firstname: nil)
contact.valid?
expect(contact.errors[:firstname]).to include("can't be blank")
end

it "is invalid without a lastname" do
contact = Contact.new(lastname: nil)
contact = build(:contact, lastname: nil)
contact.valid?
expect(contact.errors[:lastname]).to include("can't be blank")
end

it "is invalid without an email address" do
contact = Contact.new(email: nil)
contact = build(:contact, email: nil)
contact.valid?
expect(contact.errors[:email]).to include("can't be blank")
end

it "is invalid with a duplicate email address" do
Contact.create(
firstname: 'Joe', lastname: 'Tester',
email: '[email protected]'
)
contact = Contact.new(
firstname: 'Jane', lastname: 'Tester',
email: '[email protected]'
it "returns a contact's full name as a string" do
contact = build(:contact,
firstname: "Jane",
lastname: "Smith"
)
contact.valid?
expect(contact.errors[:email]).to include("has already been taken")
expect(contact.name).to eq 'Jane Smith'
end

it "returns a contact's full name as a string" do
contact = Contact.new(
firstname: 'John',
lastname: 'Doe',
email: '[email protected]'
)
expect(contact.name).to eq 'John Doe'
it "has three phone numbers" do
expect(create(:contact).phones.count).to eq 3
end

it "is invalid with a duplicate email address" do
create(:contact, email: '[email protected]')
contact = build(:contact, email:'[email protected]')
contact.valid?
expect(contact.errors[:email]).to include('has already been taken')
end

describe "filter last name by letter" do
before :each do
@smith = Contact.create(
@smith = create(:contact,
firstname: 'John',
lastname: 'Smith',
email: '[email protected]'
)
@jones = Contact.create(
@jones = create(:contact,
firstname: 'Tim',
lastname: 'Jones',
email: '[email protected]'
)
@johnson = Contact.create(
@johnson = create(:contact,
firstname: 'John',
lastname: 'Johnson',
email: '[email protected]'
Expand Down
31 changes: 7 additions & 24 deletions spec/models/phone_spec.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
require 'rails_helper'

describe Phone do
it "does not allow duplicate phone numbers per contact" do
contact = Contact.create(
firstname: 'Joe',
lastname: 'Tester',
email: '[email protected]'
)
contact.phones.create(
phone_type: 'home',
contact = create(:contact)
create(:home_phone,
contact: contact,
phone: '785-555-1234'
)
mobile_phone = contact.phones.build(
phone_type: 'mobile',
mobile_phone = build(:mobile_phone,
contact: contact,
phone: '785-555-1234'
)

Expand All @@ -21,21 +16,9 @@
end

it "allows two contacts to share a phone number" do
contact = Contact.create(
firstname: 'Joe',
lastname: 'Tester',
email: '[email protected]'
)
contact.phones.create(
phone_type: 'home',
create(:home_phone,
phone: '785-555-1234'
)
other_contact = Contact.new
other_phone = other_contact.phones.build(
phone_type: 'home',
phone: '785-555-1234'
)

expect(other_phone).to be_valid
expect(build(:home_phone, phone: "785-555-1234")).to be_valid
end
end
6 changes: 5 additions & 1 deletion spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@
ActiveRecord::Migration.maintain_test_schema!

RSpec.configure do |config|

# Include Factory Girl syntax to simplify calls to factories
config.include FactoryGirl::Syntax::Methods

# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/spec/fixtures"
# config.fixture_path = "#{::Rails.root}/spec/fixtures"

# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
Expand Down