Skip to content

Instantly share code, notes, and snippets.

@dougal
Created May 25, 2011 22:43
Show Gist options
  • Select an option

  • Save dougal/992170 to your computer and use it in GitHub Desktop.

Select an option

Save dougal/992170 to your computer and use it in GitHub Desktop.
Factory Girl vs User.create
phoenix:stats [master*]$ rails runner 'Benchmark.bm {|x| x.report { 1000.times { Factory.create(:user) } } }'
user system total real
5.050000 0.890000 5.940000 ( 7.895676)
phoenix:stats [master*]$ rails runner 'Benchmark.bm {|x| x.report { 1000.times { |i| User.create(:email => "me@example#{i}.com", :name => "bob", :password => "password1") } } }'
user system total real
5.110000 0.920000 6.030000 ( 8.807853)
ruby 1.8.7 (2011-02-18 patchlevel 334) [i686-darwin10.6.0]
Rails 3.1.0.rc1
sqlite3 (1.3.3)
factory_girl_rails (1.0.1)
class User < ActiveRecord::Base
include Clearance::User
# Used on settings screen to display a field for current password.
attr_reader :current_password
validates :name, :presence => true
validates :time_zone, :presence => true
has_many :projects
end
# All three of these attributes are required. Authentication setup by clearance. https://github.com/thoughtbot/clearance
Factory.define :user do |user|
user.name "Bob Smith"
user.sequence(:email) { |n| "user#{n}@example.com" }
user.password "password"
end
@TheEmpty
Copy link
Copy Markdown

TheEmpty commented Jun 6, 2011

FactoryGirl is a great tool and much better then building the objects. For example when we switched the way we saved associations it just required two line changes and all our tests still passed. If we have to go over every time we built it... dear Lord. Not to mention that we are always adding new fields and validations. I'll have to give this a shot on my own project soon as it has a much more complex user model.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment