1. 설정
2. 모델 작성
3. Rorla_api 실습
contacts.yaml : Contact 모델에 대한 샘플 데이타
contacts(:aaron)과 같이 테스트 코드에 사용함.
픽스처은 데이타 유효성 검증을 하지 않아도 사용할 수 있고
팩토리는 복잡한 스키마에서 처리속도가 느림.
aaron:firstname: "Aaron" lastname: "Sumner" email: "aaron@everydayrails.com" john: firstname: "John" lastname: "Doe" email: "johndoe@nobody.org"
FactoryGirl.define do factory :contact do firstname "John" astname "Doe" sequence(:email) { |n| "johndoe#{n}@example.com"}#johndoe1@example.com, johndoe2@example.comend end
require'spec_helper' describe Contact do it "has a valid factory" do expect(FactoryGirl.build(:contact)).to be_valid # build(new), create(save)# contact = Contact.new(firstname: 'Aaron', lastname: 'Sumner', email: 'tester@example.com') endend
RSpec.configuredo|config| # Include Factory Girl syntax to simplify calls to factoriesconfig.include FactoryGirl::Syntax::Methods # other configurations omitted ...# build(:contact), create(:contact), attributes_for(:contact), build_stubbed(:contact) end
it "is invalid without a firstname" do
contact = FactoryGirl.build(:contact, firstname: nil) expect(contact).to have(1).errors_on(:firstname)
end
it "is invalid without a lastname" do
contact = FactoryGirl.build(:contact, lastname: nil) expect(contact).to have(1).errors_on(:lastname)
end