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
67
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
93
Lambdas and Pops - Jan Hettich
lvrug
0
65
Making Good Use of Fonts - Russ Smith
lvrug
1
78
Featured
See All Featured
Automating Front-end Workflow
addyosmani
1370
200k
The Cost Of JavaScript in 2023
addyosmani
51
8.4k
The Language of Interfaces
destraynor
158
25k
Designing Experiences People Love
moore
142
24k
What’s in a name? Adding method to the madness
productmarketing
PRO
23
3.5k
Java REST API Framework Comparison - PWX 2021
mraible
31
8.6k
Six Lessons from altMBA
skipperchong
28
3.8k
RailsConf 2023
tenderlove
30
1.1k
Faster Mobile Websites
deanohume
307
31k
Fantastic passwords and where to find them - at NoRuKo
philnash
51
3.3k
How To Stay Up To Date on Web Technology
chriscoyier
790
250k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
667
120k
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*