Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Factory Girl - Gabe Evans
Search
Las Vegas Ruby Group
January 09, 2013
0
86
Factory Girl - Gabe Evans
Las Vegas Ruby Group
January 09, 2013
Tweet
Share
More Decks by Las Vegas Ruby Group
See All by Las Vegas Ruby Group
Ruby ISO Standard - David Grayson
lvrug
0
92
Windows Automation - Howard Feldman
lvrug
0
37
Separating Your Application from Rails - Brian Hughes
lvrug
0
70
SWIG and Ruby - David Grayson
lvrug
0
39
Practical Object-Oriented Design in Ruby - Charles Jackson
lvrug
3
96
The Hamster Gem - Ryan Mulligan
lvrug
1
70
Varnish+Redis - Russ Smith
lvrug
1
73
Lambdas and Pops - Jan Hettich
lvrug
0
38
Making Good Use of Fonts - Russ Smith
lvrug
1
53
Featured
See All Featured
Atom: Resistance is Futile
akmur
258
25k
Rails Girls Zürich Keynote
gr2m
91
13k
A Philosophy of Restraint
colly
195
15k
Building Effective Engineering Teams - LeadDev
addyosmani
25
1.8k
Side Projects
sachag
451
41k
Building Flexible Design Systems
yeseniaperezcruz
317
37k
Build The Right Thing And Hit Your Dates
maggiecrowley
23
1.9k
Unsuck your backbone
ammeep
660
56k
Pencils Down: Stop Designing & Start Developing
hursman
115
11k
WebSockets: Embracing the real-time Web
robhawkes
59
6.9k
Designing on Purpose - Digital PM Summit 2013
jponch
109
6.4k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
34
8.8k
Transcript
Factory Girl A Brief Introduction Using Ruby on Rails
What’s Factory Girl?
A Replacement for Fixtures
Provides a Simple DSL
Keeps Tests Focused & Readable
Builds Objects Instead of Database Records
Installation
Gemfile
group :development, :test do gem ‘factory_girl_rails’ end Gemfile
$ bundle install
Defining a Factory
spec/factories/
spec/factories/users.rb
spec/factories/posts.rb
spec/factories/comments.rb
spec/factories/products.rb
spec/factories/users.rb
FactoryGirl.define do factory :user do name ‘Joe Blow’ email ‘
[email protected]
’
password ‘p@ssw0rd’ admin false end end spec/factories/users.rb
spec/models/user_spec.rb
describe User do end spec/models/user_spec.rb
describe User do subject { FactoryGirl.create(:user) } end spec/models/user_spec.rb
describe User do subject { FactoryGirl.create(:user) } its(:name) { should
== ‘Joe Blow’ } end spec/models/user_spec.rb
describe User do subject { FactoryGirl.create(:user) } its(:name) { should
== ‘Joe Blow’ } its(:email) { should == ‘
[email protected]
’ } end spec/models/user_spec.rb
describe User do subject { FactoryGirl.create(:user) } its(:name) { should
== ‘Joe Blow’ } its(:email) { should == ‘
[email protected]
’ } it { should_not be_admin } end spec/models/user_spec.rb
FactoryGirl.create(:user)
FactoryGirl.build(:user)
FactoryGirl.build_stubbed(:user)
spec/spec_helper.rb
RSpec.configure do |config| config.include FactoryGirl::Syntax::Methods end spec/spec_helper.rb
FactoryGirl.create(:user)
create(:user)
build(:user)
build_stubbed(:user)
spec/models/user_spec.rb describe User do subject { create(:user) } its(:name) {
should == ‘Joe Blow’ } its(:email) { should == ‘
[email protected]
’ } it { should_not be_admin } end
Overriding Attributes
spec/models/user_spec.rb context ‘a duplicate email address’ do it { should_not
be_valid } end
context ‘a duplicate email address’ do let(:user) { create(:user) }
subject { User.new(name: ‘Joe Duplicate’, email: user.email, password: ‘test123’) } it { should_not be_valid } end spec/models/user_spec.rb
context ‘a duplicate email address’ do let(:user) { create(:user) }
subject { build(:user, email: user.email) } it { should_not be_valid } end spec/models/user_spec.rb
Lazy Attributes
spec/factories/posts.rb FactoryGirl.define do factory :post do title ‘Worst presentation EVER!’
content “Gabe’s presentations are long-winded. After the first 40 slides I passed out.” published_at { Time.now } end end
Sequences
FactoryGirl.define do sequence :email do |n| “person#{n}@example.com” end end spec/factories/sequences.rb
Traits
Associations
Dependent Attributes
Transient Attributes
Creating or Building Multiple Objects
Callbacks
A Real World Scenario
Wrapping Up...
Questions?
*applause*