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
90
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
99
Windows Automation - Howard Feldman
lvrug
0
38
Separating Your Application from Rails - Brian Hughes
lvrug
0
89
SWIG and Ruby - David Grayson
lvrug
0
44
Practical Object-Oriented Design in Ruby - Charles Jackson
lvrug
3
100
The Hamster Gem - Ryan Mulligan
lvrug
1
77
Varnish+Redis - Russ Smith
lvrug
1
76
Lambdas and Pops - Jan Hettich
lvrug
0
43
Making Good Use of Fonts - Russ Smith
lvrug
1
58
Featured
See All Featured
How to train your dragon (web standard)
notwaldorf
88
5.7k
A Philosophy of Restraint
colly
203
16k
Docker and Python
trallard
40
3.1k
Building Adaptive Systems
keathley
38
2.3k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
250
21k
Fontdeck: Realign not Redesign
paulrobertlloyd
82
5.2k
We Have a Design System, Now What?
morganepeng
50
7.2k
StorybookのUI Testing Handbookを読んだ
zakiyama
27
5.3k
Building Better People: How to give real-time feedback that sticks.
wjessup
364
19k
Fantastic passwords and where to find them - at NoRuKo
philnash
50
2.9k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
10
720
The Pragmatic Product Professional
lauravandoore
31
6.3k
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*