github
Advanced Search
  • Home
  • Pricing and Signup
  • Explore GitHub
  • Blog
  • Login

thoughtbot / factory_girl

  • Admin
  • Watch Unwatch
  • Fork
  • Your Fork
  • Pull Request
  • Download Source
    • 978
    • 85
  • Source
  • Commits
  • Network (85)
  • Issues (22)
  • Downloads (10)
  • Wiki (4)
  • Graphs
  • Branch: master

click here to add a description

click here to add a homepage

  • Branches (3)
    • 2.0
    • master ✓
    • rails3
  • Tags (10)
    • rel_1-2-2
    • rel_1-2-1
    • rel_1-2-0
    • rel_1-1-5
    • rel_1-1-4
    • rel_1-1-3
    • rel_1-1-2
    • rel_1-1-1
    • rel_1-1
    • rel_1-0
Sending Request…
Enable Donations

Pledgie Donations

Once activated, we'll place the following badge in your repository's detail box:
Pledgie_example
This service is courtesy of Pledgie.

Fixture replacement for focused and readable tests. — Read more

  cancel

http://thoughtbot.com/projects/factory_girl

  cancel
  • Private
  • Read-Only
  • HTTP Read-Only

This URL has Read+Write access

README points to gemcutter and typo fix Comment
stevenderocher (author)
Wed Dec 16 10:19:10 -0800 2009
commit  3287ed8dee95a0540db409eea8cac3531a04f3cd
tree    be7e847783a7ebd08614488244328ece74c43310
parent  690102afd2adef5be4137e50977b8871fec2394f
factory_girl /
name age
history
message
file .autotest Sun Jun 01 11:14:21 -0700 2008 Added autotest mappings [jferris]
file .gitignore Fri Nov 28 11:48:13 -0800 2008 Added rcov to the mix. [technicalpickles]
file CONTRIBUTION_GUIDELINES.rdoc Wed Jun 10 07:31:12 -0700 2009 Updating readme and contribution guidelines wit... [qrush]
file Changelog Fri Nov 28 14:03:53 -0800 2008 Updated the Changelog [Joe Ferris]
file LICENSE Sun Jun 01 11:27:59 -0700 2008 Added a license file [jferris]
file README.rdoc Wed Dec 16 10:19:10 -0800 2009 README points to gemcutter and typo fix [stevenderocher]
file Rakefile Fri Oct 16 11:13:13 -0700 2009 proper gemspec for 1.2.3 [cpytel]
file cucumber.yml Tue Sep 15 13:56:20 -0700 2009 Fixed issues with some attributes being skipped... [Joe Ferris]
file factory_girl.gemspec Fri Oct 16 11:13:13 -0700 2009 proper gemspec for 1.2.3 [cpytel]
directory features/ Tue Sep 15 15:14:34 -0700 2009 Fixes for step definitions involving factories ... [Joe Ferris]
directory lib/ Mon Nov 23 07:33:30 -0800 2009 Attempting to define multiple factories with th... [r00k]
directory spec/ Mon Nov 23 07:33:30 -0800 2009 Attempting to define multiple factories with th... [r00k]
README.rdoc

factory_girl

factory_girl is a fixtures replacement with a straightforward definition syntax, support for multiple build strategies (saved instances, unsaved instances, attribute hashes, and stubbed objects), and support for multiple factories for the same class (user, admin_user, and so on), including factory inheritance.

Download

Github: github.com/thoughtbot/factory_girl/tree/master

Gem:

  gem install factory_girl --source http://gemcutter.org

Note: if you install factory_girl using the gem from Gemcutter, you’ll need this in your environment.rb if you want to use Rails 2.1+’s dependency manager:

  config.gem "factory_girl",
                         :source => "http://gemcutter.org"

Defining factories

Each factory has a name and a set of attributes. The name is used to guess the class of the object by default, but it’s possible to explicitly specify it:

  # This will guess the User class
  Factory.define :user do |u|
    u.first_name 'John'
    u.last_name  'Doe'
    u.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

  # The same, but using a string instead of class constant
  Factory.define :admin, :class => 'user' do |u|
    u.first_name 'Admin'
    u.last_name  'User'
    u.admin true
  end

It is highly recommended that you have one factory for each class that provides the simplest set of attributes necessary to create an instance of that class. If you’re creating ActiveRecord objects, that means that you should only provide attributes that are required through validations and that do not have defaults. Other factories can be created through inheritance to cover common scenarios for each class.

Attempting to define multiple factories with the same name will raise an error.

Factories can either be defined anywhere, but will automatically be loaded if they are defined in files at the following locations:

  test/factories.rb
  spec/factories.rb
  test/factories/*.rb
  spec/factories/*.rb

Using factories

factory_girl supports several different build strategies: build, create, attributes_for and stub:

  # Returns a User instance that's not saved
  user = Factory.build(:user)

  # Returns a saved User instance
  user = Factory.create(:user)

  # Returns a hash of attributes that can be used to build a User instance:
  attrs = Factory.attributes_for(:user)

  # Returns an object with all defined attributes stubbed out:
  stub = Factory.stub(:user)

You can use the Factory method as a shortcut for the default build strategy:

  # Same as Factory.create :user:
  user = Factory(:user)

The default strategy can be overriden:

  # Now same as Factory.build(:user)
  Factory.define :user, :default_strategy => :build do |u|
    ...
  end

  user = Factory(:user)

No matter which strategy is used, it’s possible to override the defined attributes by passing a hash:

  # Build a User instance and override the first_name property
  user = Factory.build(:user, :first_name => 'Joe')
  user.first_name
  # => "Joe"

Lazy Attributes

Most factory attributes can be added using static values that are evaluated when the factory is defined, but some attributes (such as associations and other attributes that must be dynamically generated) will need values assigned each time an instance is generated. These "lazy" attributes can be added by passing a block instead of a parameter:

  Factory.define :user do |u|
    # ...
    u.activation_code { User.generate_activation_code }
  end

Dependent Attributes

Attributes can be based on the values of other attributes using the proxy that is yieled to lazy attribute blocks:

  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

  Factory(:user, :last_name => 'Doe').email
  # => "joe.doe@example.com"

Associations

Associated instances can be generated by using the association method when defining a lazy attribute:

  Factory.define :post do |p|
    # ...
    p.author {|author| author.association(:user, :last_name => 'Writely') }
  end

The behavior of the association method varies depending on the build strategy used for the parent object.

  # Builds and saves a User and a Post
  post = Factory(:post)
  post.new_record?       # => false
  post.author.new_record # => false

  # Builds and saves a User, and then builds but does not save a Post
  Factory.build(:post)
  post.new_record?       # => true
  post.author.new_record # => false

Because this pattern is so common, a prettier syntax is available for defining associations:

  # The following definitions are equivilent:
  Factory.define :post do |p|
    p.author {|a| a.association(:user) }
  end

  Factory.define :post do |p|
    p.association :author, :factory => :user
  end

If the factory name is the same as the association name, the factory name can be left out.

Inheritance

You can easily create multiple factories for the same class without repeating common attributes by using inheritance:

  Factory.define :post do |p|
    # the 'title' attribute is required for all posts
    p.title 'A title'
  end

  Factory.define :approved_post, :parent => :post do |p|
    p.approved true
    # the 'approver' association is required for an approved post
    p.association :approver, :factory => :user
  end

Sequences

Unique values in a specific format (for example, e-mail addresses) can be generated using sequences. Sequences are defined by calling Factory.sequence, and values in a sequence are generated by calling Factory.next:

  # Defines a new sequence
  Factory.sequence :email do |n|
    "person#{n}@example.com"
  end

  Factory.next :email
  # => "person1@example.com"

  Factory.next :email
  # => "person2@example.com"

Sequences can be used in lazy attributes:

  Factory.define :user do |f|
    f.email { Factory.next(:email) }
  end

And it’s also possible to define an in-line sequence that is only used in a particular factory:

  Factory.define :user do |f|
    f.sequence(:email) {|n| "person#{n}@example.com" }
  end

Callbacks

Factory_girl makes available three callbacks for injecting some code:

  • after_build - called after a factory is built (via Factory.build)
  • after_create - called after a factory is saved (via Factory.create)
  • after_stub - called after a factory is stubbed (via Factory.stub)

Examples:

  # Define a factory that calls the generate_hashed_password method after it is built
  Factory.define :user do |u|
    u.after_build { |user| do_something_to(user) }
  end

Note that you’ll have an instance of the user in the block. This can be useful.

You can also define multiple types of callbacks on the same factory:

  Factory.define :user do |u|
    u.after_build  { |user| do_something_to(user) }
    u.after_create { |user| do_something_else_to(user) }
  end

Factories can also define any number of the same kind of callback. These callbacks will be executed in the order they are specified:

  Factory.define :user do |u|
    u.after_create { this_runs_first }
    u.after_create { then_this }
  end

Calling Factory.create will invoke both after_build and after_create callbacks.

Also, like standard attributes, child factories will inherit (and can define additional) callbacks from their parent factory.

Alternate Syntaxes

Users’ tastes for syntax vary dramatically, but most users are looking for a common feature set. Because of this, factory_girl supports "syntax layers" which provide alternate interfaces. See Factory::Syntax for information about the various layers available.

More Information

Our blog: giantrobots.thoughtbot.com

factory_girl rdoc: rdoc.info/projects/thoughtbot/factory_girl

Mailing list: groups.google.com/group/factory_girl

factory_girl issues: github.com/thoughtbot/factory_girl/issues

Contributing

Please read the contribution guidelines before submitting patches or pull requests.

Author

factory_girl was written by Joe Ferris with contributions from several authors, including:

  • Alex Sharp
  • Eugene Bolshakov
  • Jon Yurek
  • Josh Nichols
  • Josh Owens
  • Nate Sutton

The syntax layers are derived from software written by the following authors:

  • Pete Yandell
  • Rick Bradley
  • Yossef Mendelssohn

Thanks to all members of thoughtbot for inspiration, ideas, and funding.

Copyright 2008-2009 Joe Ferris and thoughtbot, inc.

Blog | Support | Training | Contact | API | Status | Twitter | Help | Security
© 2010 GitHub Inc. All rights reserved. | Terms of Service | Privacy Policy
Powered by the Dedicated Servers and
Cloud Computing of Rackspace Hosting®
Dedicated Server