≡

wincent.dev

  • Products
  • Blog
  • Wiki
  • Issues
You are viewing an historical archive of past issues. Please report new issues to the appropriate project issue tracker on GitHub.
Home » Issues » Feature request #1572

Feature request #1572: Rails 3: most likely switch to Factory Girl

Kind feature request
Product wincent.dev
When Created 2010-06-15T05:58:53Z, updated 2010-06-17T07:16:01Z
Status closed
Reporter Greg Hurrell
Tags no tags

Description

I like the flexibility of Factory Girl (seems to handle things like associations and sequences and edge cases very well), but I don't like the actual API for generating instances that you use in tests.

For example, compare FixtureReplacement:

it 'should quack like a duck' do
  new_user.quack.should == 'ducky quack'
end

With Factory Girl:

it 'should quack like a duck' do
  Factory.build(:user).quack.should == 'ducky quack'
end

So the FixtureReplacement one pollutes the example namespace with lots of methods like new_user and create_user, but the Factory Girl API just strikes me as ugly and overly verbose. You want instance set-up to be dead easy in testing; that's the whole point of factories.

Machinist seems to strike a nice compromise by adding a single make method to ActiveRecord models (actually, it also adds a plan method but the make method is of principal interest here):

it 'should quack like a duck' do
  User.make.quack.should == 'ducky quack'
end

To flesh that out a bit more, overrides look like this:

User.make(:name => 'bob')

To me this seems like a nice compromise between minimal "pollution" and maximum ease of use and readability.

So I am thinking of using Factory Girl, but adding my own "helper" methods to ActiveRecord models so that I can generate instances the way I like.

Basically, my make method would end up calling Factory.build behind the scenes.

Comments

  1. Greg Hurrell 2010-06-15T13:38:11Z

    Ok, I've added the make convenience wrapper. Going to test it out for a while and see if switching to Factory Girl is a viable/worthwhile idea.

  2. Greg Hurrell 2010-06-15T13:48:25Z

    Hay, funnily enough, I just discovered that there already is supported for the Machinist-like syntax baked in to Factory Girl:

    • http://github.com/thoughtbot/factory_girl/blob/master/lib/factory_girl/syntax/make.rb
  3. Greg Hurrell 2010-06-17T07:15:56Z

    Ok, this is basically done now. Marking as closed.

  4. Greg Hurrell 2010-06-17T07:16:01Z

    Status changed:

    • From: new
    • To: closed
Add a comment

Comments are now closed for this issue.

  • contact
  • legal

Menu

  • Blog
  • Wiki
  • Issues
  • Snippets