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
92
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
64
Separating Your Application from Rails - Brian Hughes
lvrug
0
110
SWIG and Ruby - David Grayson
lvrug
0
72
Practical Object-Oriented Design in Ruby - Charles Jackson
lvrug
3
120
The Hamster Gem - Ryan Mulligan
lvrug
1
86
Varnish+Redis - Russ Smith
lvrug
1
99
Lambdas and Pops - Jan Hettich
lvrug
0
72
Making Good Use of Fonts - Russ Smith
lvrug
1
83
Featured
See All Featured
Statistics for Hackers
jakevdp
799
220k
Build The Right Thing And Hit Your Dates
maggiecrowley
37
2.8k
Designing Experiences People Love
moore
142
24k
KATA
mclloyd
32
14k
The World Runs on Bad Software
bkeepers
PRO
70
11k
Code Review Best Practice
trishagee
70
19k
[RailsConf 2023] Rails as a piece of cake
palkan
57
5.8k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
231
53k
What's in a price? How to price your products and services
michaelherold
246
12k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
16k
Building an army of robots
kneath
306
46k
Speed Design
sergeychernyshev
32
1.1k
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*