Factory Girl - Gabe Evans

B044a0f039af800f4df09bf3b2465f18?s=47 Las Vegas Ruby Group
January 09, 2013
71

Factory Girl - Gabe Evans

B044a0f039af800f4df09bf3b2465f18?s=128

Las Vegas Ruby Group

January 09, 2013
Tweet

Transcript

  1. Factory Girl A Brief Introduction Using Ruby on Rails

  2. What’s Factory Girl?

  3. A Replacement for Fixtures

  4. Provides a Simple DSL

  5. Keeps Tests Focused & Readable

  6. Builds Objects Instead of Database Records

  7. Installation

  8. Gemfile

  9. group :development, :test do gem ‘factory_girl_rails’ end Gemfile

  10. $ bundle install

  11. Defining a Factory

  12. spec/factories/

  13. spec/factories/users.rb

  14. spec/factories/posts.rb

  15. spec/factories/comments.rb

  16. spec/factories/products.rb

  17. spec/factories/users.rb

  18. FactoryGirl.define do factory :user do name ‘Joe Blow’ email ‘joe.blow@gmail.com’

    password ‘p@ssw0rd’ admin false end end spec/factories/users.rb
  19. spec/models/user_spec.rb

  20. describe User do end spec/models/user_spec.rb

  21. describe User do subject { FactoryGirl.create(:user) } end spec/models/user_spec.rb

  22. describe User do subject { FactoryGirl.create(:user) } its(:name) { should

    == ‘Joe Blow’ } end spec/models/user_spec.rb
  23. describe User do subject { FactoryGirl.create(:user) } its(:name) { should

    == ‘Joe Blow’ } its(:email) { should == ‘joe.blow@gmail.com’ } end spec/models/user_spec.rb
  24. describe User do subject { FactoryGirl.create(:user) } its(:name) { should

    == ‘Joe Blow’ } its(:email) { should == ‘joe.blow@gmail.com’ } it { should_not be_admin } end spec/models/user_spec.rb
  25. FactoryGirl.create(:user)

  26. FactoryGirl.build(:user)

  27. FactoryGirl.build_stubbed(:user)

  28. spec/spec_helper.rb

  29. RSpec.configure do |config| config.include FactoryGirl::Syntax::Methods end spec/spec_helper.rb

  30. FactoryGirl.create(:user)

  31. create(:user)

  32. build(:user)

  33. build_stubbed(:user)

  34. spec/models/user_spec.rb describe User do subject { create(:user) } its(:name) {

    should == ‘Joe Blow’ } its(:email) { should == ‘joe.blow@gmail.com’ } it { should_not be_admin } end
  35. Overriding Attributes

  36. spec/models/user_spec.rb context ‘a duplicate email address’ do it { should_not

    be_valid } end
  37. 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
  38. 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
  39. Lazy Attributes

  40. 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
  41. Sequences

  42. FactoryGirl.define do sequence :email do |n| “person#{n}@example.com” end end spec/factories/sequences.rb

  43. Traits

  44. Associations

  45. Dependent Attributes

  46. Transient Attributes

  47. Creating or Building Multiple Objects

  48. Callbacks

  49. A Real World Scenario

  50. Wrapping Up...

  51. Questions?

  52. *applause*