diff --git a/app/models/contact.rb b/app/models/contact.rb index da95110b..8c7153a2 100644 --- a/app/models/contact.rb +++ b/app/models/contact.rb @@ -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(' ') diff --git a/spec/factories/contacts.rb b/spec/factories/contacts.rb new file mode 100644 index 00000000..2872ab3f --- /dev/null +++ b/spec/factories/contacts.rb @@ -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 diff --git a/spec/factories/phones.rb b/spec/factories/phones.rb new file mode 100644 index 00000000..7ea487b5 --- /dev/null +++ b/spec/factories/phones.rb @@ -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 + diff --git a/spec/models/contact_spec.rb b/spec/models/contact_spec.rb index 523f8ad9..92e127cb 100644 --- a/spec/models/contact_spec.rb +++ b/spec/models/contact_spec.rb @@ -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: 'tester@example.com') - 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: 'tester@example.com') + # 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: 'tester@example.com' - ) - contact = Contact.new( - firstname: 'Jane', lastname: 'Tester', - email: 'tester@example.com' + 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: 'johndoe@example.com' - ) - 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: 'aaron@example.com') + contact = build(:contact, email:'aaron@example.com') + 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: 'jsmith@example.com' ) - @jones = Contact.create( + @jones = create(:contact, firstname: 'Tim', lastname: 'Jones', email: 'tjones@example.com' ) - @johnson = Contact.create( + @johnson = create(:contact, firstname: 'John', lastname: 'Johnson', email: 'jjohnson@example.com' diff --git a/spec/models/phone_spec.rb b/spec/models/phone_spec.rb index 37f7260d..b0de0cbf 100644 --- a/spec/models/phone_spec.rb +++ b/spec/models/phone_spec.rb @@ -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: 'joetester@example.com' - ) - 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' ) @@ -21,21 +16,9 @@ end it "allows two contacts to share a phone number" do - contact = Contact.create( - firstname: 'Joe', - lastname: 'Tester', - email: 'joetester@example.com' - ) - 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 diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index b98cb0f7..c9f74ae1 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -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