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
91
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
120
Windows Automation - Howard Feldman
lvrug
0
59
Separating Your Application from Rails - Brian Hughes
lvrug
0
110
SWIG and Ruby - David Grayson
lvrug
0
66
Practical Object-Oriented Design in Ruby - Charles Jackson
lvrug
3
120
The Hamster Gem - Ryan Mulligan
lvrug
1
82
Varnish+Redis - Russ Smith
lvrug
1
92
Lambdas and Pops - Jan Hettich
lvrug
0
64
Making Good Use of Fonts - Russ Smith
lvrug
1
77
Featured
See All Featured
Designing for Performance
lara
609
69k
The Cult of Friendly URLs
andyhume
79
6.4k
The Invisible Side of Design
smashingmag
299
51k
The Illustrated Children's Guide to Kubernetes
chrisshort
48
50k
Intergalactic Javascript Robots from Outer Space
tanoku
271
27k
Speed Design
sergeychernyshev
31
990
Building Better People: How to give real-time feedback that sticks.
wjessup
367
19k
How to Think Like a Performance Engineer
csswizardry
24
1.7k
Producing Creativity
orderedlist
PRO
346
40k
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
3.9k
Bootstrapping a Software Product
garrettdimon
PRO
307
110k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
233
17k
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*