-
-
Save davepoirier/5691729 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# This will guess the User class | |
Factory.define :user do | |
first_name 'John' | |
last_name 'Doe' | |
admin false | |
end | |
# This will use the User class (Admin would have been guessed) | |
Factory.define :admin, :class => User do |u| | |
u.first_name 'Admin' | |
u.last_name 'User' | |
u.admin true | |
end | |
# Lazy Attributes: | |
Factory.define :user do |u| | |
# ... | |
u.activation_code { User.generate_activation_code } | |
end | |
# Dependent Attributes: | |
Factory.define :user do |u| | |
u.first_name 'Joe' | |
u.last_name 'Blow' | |
u.email {|a| "#{a.first_name}.#{a.last_name}@example.com".downcase } | |
end | |
# Associations | |
Factory.define :post do |p| | |
# ... | |
p.author {|author| author.association(:user, :last_name => 'Writely') } | |
end | |
# Or in short form | |
Factory.define :post do |p| | |
p.association :author, :factory => :user | |
end | |
# Defines a new sequence | |
Factory.sequence :email do |n| | |
"person#{n}@example.com" | |
end | |
# Uses the sequence | |
Factory.next :email | |
# => "[email protected]" | |
# Build and save a User instance | |
Factory(:user) | |
# Build a User instance and override the first_name property | |
Factory.build(:user, :first_name => 'Joe') | |
# Return an attributes Hash that can be used to build a User instance | |
attrs = Factory.attributes_for(:user) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment