Created
May 25, 2011 22:43
-
-
Save dougal/992170 to your computer and use it in GitHub Desktop.
Factory Girl vs User.create
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
My point was that you were being rude about someone elses project. Criticism is fine.
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
Nonetheless, I just started using Ruby so I obviously won't have any Ruby/github projects. All of my experience has been in the Java world, and I have written/contributed to many open-source projects in this space.