RPSEC 과 함께 걸음마를

Title

rake rspec
rspec with rubytest sublime
spork
--drb
--format documetation

지난 시간에

1. 설정

2.  모델 작성

3. Rorla_api 실습

RSPEC 설정

모델 스펙 작성

RorLA_API 실습

팩토리의 개요

  1. 팩토리는 루비 오브젝트를 테스트 데이타로 사용
  2. 팩토리와 픽스처를 비교
  3. 팩토리를  추가
  4. 팩토리 구문 생략 방법
  5. 페이커 젬을 사용하여 실제같은 데이타를 생성
  6. 액티브 레코드의 관계(Association)
  7. 팩토리를 잘 못 사용한 예

픽스처와 비교

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.com  end
end
spec/factories/contacts.rb 
FactoryGirl.create(:contact) 와 같이 테스트 코드에 사용. 
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
spec/models/contact_spec.rb

팩토리 줄여서 쓰기

RSpec.configuredo|config|
  # Include Factory Girl syntax to simplify calls to factories  config.include FactoryGirl::Syntax::Methods
  # other configurations omitted ...  #  build(:contact), create(:contact), attributes_for(:contact), build_stubbed(:contact)
end
spec/spec_helper.rb

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

액티브 렉코드 관계계



팩토리 설정

팩토리 실습

rspec-factory

By wagurano

rspec-factory

루비 온 레일즈 RSPEC, 팩토리 사용

  • 1,137