Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Head First Fixture Replacement

Head First Fixture Replacement

ペパボ社内の RSpec 勉強会で Fixture Replacement について発表したスライドです。

Kensuke Nagae

March 21, 2012
Tweet

More Decks by Kensuke Nagae

Other Decks in Programming

Transcript

  1. : ( user = User.create user.albums << Album.create user.albums <<

    Album.create user.albums << Album.create assert user.albums.count >= 3
  2. # User has_many :bookmarks has_many :pages, :through => :bookmarks #

    Page has_many :bookmarks has_many :users, :through => :bookmarks # Bookmark belongs_to :user belongs_to: page
  3. # factories/users.rb Factory.define(:album) do |a| a.title "Album Title" a.password "Album

    Password" end album = Factory.create(:album) album = Factory.create(:album, :title => "Happy Wedding")
  4. # factories/users.rb Factory.define(:alice) do |u| u.pages {[ Factory(:twitter) ]} end

    Factory.define(:bob) do |u| u.pages {[ Factory(:facebook) ]} end
  5. # factories/albums.rb Factory.define(:album) do |a| a.user { Factory(:user) } end

    Factory.define(:paid) do |a| a.user { Factory(:user) } end
  6. user = Factory.create(:user) 3.times do Factory(:album, :user => user) end

    Factory(:paid, :user => user) assert user.albums.count == 4 # SUCCESS
  7. # factories/photos.rb exif = { :width => 1024, :height =>

    768 } Factory.define(:photo) do |p| p.exif { exif.to_yaml } end
  8. end