# 소스 받기$ git clone https://github.com/everydayrails/rspec_rails_4.git$ cd rspec_rails_4$ bundle install# 03_models 브랜치 이동$ git checkout -b 03_models origin/03_models# db 생성, Rspec 인스톨 후, Rspec 실행$ bundle exec rake db:create:all$ bundle exec rails generate rspec:install$ bundle exec rake db:test:clone$ bundle exec rspec
group :development, :test do
gem "rspec-rails", "~> 2.14.0"
gem "factory_girl_rails", "~> 4.2.1"
end
group :test do
gem "faker", "~> 1.1.2"
gem "capybara", "~> 2.1.0"
gem "database_cleaner", "~> 1.0.1"
gem "launchy", "~> 2.3.0"
gem "selenium-webdriver", "~> 2.35.1"
end
factory_girl : Rails의 테스트 데이터를 더욱 유연하게 만듭니다.
faker: 이름, 이메일 등을 실제 데이터같이 생성합니다.
capybara: 웹 어플리케이션에서 유저와의 상호작용을 쉽게 시뮬레이션 할 수 있습니다.
database_cleaner: RSpec이 실행될때 Test DB를 지워줍니다.
launchy: 디버깅 테스트 할 때 매우 유용합니다. 기본 웹 브라우져를 실행하고 어플리케이션을 랜더링합니다.
selenium-webdriver: Capybara와 함께 유저의 브라우져 상호작용을 테스트합니다.
require 'spec_helper'
describe Contact do
it "is valid with a firstname, lastname and email"
it "is invalid without a firstname"
it "is invalid without a lastname"
it "is invalid without an email address"
it "is invalid with a duplicate email address"
it "returns a contact's full name as a string"
end
it "is invalid without a firstname" do
expect(Contact.new(firstname: nil)).to have(1).errors_on(:firstname)
end
it "is invalid without a lastname" do
expect(Contact.new(lastname: nil)).to have(1).errors_on(:lastname)
end
it "is invalid without an email address" do
expect(Contact.new(email: nil)).to have(1).errors_on(:email)
end
it "is invalid with a duplicate email address" doContact.create( firstname: 'Joe', lastname: 'Tester', email: 'tester@example.com')contact = Contact.new( firstname: 'Jane', lastname: 'Tester', email: 'tester@example.com')expect(contact).to have(1).errors_on(:email)end
describe Phone do it "does not allow duplicate phone numbers per contact" docontact = Contact.create(firstname: 'Joe', lastname: 'Tester', email: 'joetester@example.com') contact.phones.create(phone_type: 'home', phone: '785-555-1234')mobile_phone = contact.phones.build(phone_type: 'mobile', phone: '785-555-1234') expect(mobile_phone).to have(1).errors_on(:phone) 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', phone: '785-555-1234') other_contact = Contact.newother_phone = other_contact.phones.build(phone_type: 'home', phone: '785-555-1234') expect(other_phone).to be_valid end end
class Contact < ActiveRecord::Base
def name
[firstname, lastname].join(' ')
end
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'
end
def self.by_letter(letter)
where('lastname LIKE ?','#{letter}%').order(:lastname)
end
#클래스 메소드와 스코프
it "returns a sorted array of results that match" do
smith = Contact.create(firstname: 'John', lastname: 'Smith', email: 'jsmith@example.com')
jones = Contact.create(firstname: 'Tim', lastname: 'Jones', email: 'tjones@example.com')
johnson = Contact.create(firstname: 'John', lastname: 'Johnson', email: 'jjohnson@example.com')
expect(Contact.by_letter("J")).to eq [johnson, jones]
end
require 'spec_helper' describe Contact do # validation examples... describe "filter last name by letter" do # filtering examples...before :each do @smith = Contact.create(firstname: 'John', lastname: 'Smith', email: 'jsmith@example.com') @jones = Contact.create(firstname: 'Tim', lastname: 'Jones', email: 'tjones@example.com') @johnson = Contact.create(firstname: 'John', lastname: 'Johnson', email: 'jjohnson@example.com') endcontext "matching letters" do # matching examples... end context "non-matching letters" do # non-matching examples...endendend