Last active
January 3, 2016 20:09
-
-
Save jakenotjacob/8512930 to your computer and use it in GitHub Desktop.
Figuring out the correct way to deal with FactoryGirl callbacks on a has_many association.
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 Computer < ActiveRecord::Base | |
validates :make, presence: true, length: { maximum: 23 } | |
validates :model, presence: true, length: { maximum: 23 } | |
#ESN or MEID lengths should be expected, but we'll use 23 just 'cuz | |
validates :serial_number, presence: true, length: { maximum: 23 } | |
belongs_to :customer | |
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 Customer < ActiveRecord::Base | |
validates :first_name, :last_name, presence: true, length: { maximum: 23 } | |
validates :address, presence: true, length: { maximum: 95 } | |
validates :phone_number, presence: true, | |
length: { maximum: 10 }, numericality: { only_integer: true } | |
validates :zip_code, presence: true, | |
length: { maximum: 5 }, numericality: { only_integer: true } | |
has_many :computers | |
def full_name | |
"#{self.first_name} #{self.last_name}" | |
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 :customer do | |
first_name "Finn" | |
last_name "Human" | |
phone_number "1112223333" | |
address "19 Treehouse Way" | |
zip_code "54321" | |
factory :customer_with_computer do | |
after(:build) do |customer| | |
create(:computer, customer: customer) | |
end | |
end | |
end | |
factory :computer do | |
make "Asus" | |
model "X52F" | |
serial_number "A8N0AS24571899E" | |
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 'spec_helper' | |
describe "Viewing Computers" do | |
let!(:customer) { FactoryGirl.build(:customer_with_computer) } | |
computer = FactoryGirl.create(:computer) | |
before do | |
visit '/' | |
click_link "Customers" | |
click_link customer.full_name | |
end | |
describe "the Customer profile page" do | |
it "shows the list of Customer Computers" do | |
expect(page).to have_content("Computers") | |
expect(page).to have_link (computer.make + " " + computer.model) | |
end | |
end | |
describe "clicking on a Computer" do | |
it "shows the Computer specs" do | |
click_link (computer.make + " " + computer.model) | |
expect(page).to have_content(computer.make) | |
expect(page).to have_content(computer.model) | |
expect(page).to have_content(computer.serial_number) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment