Practical Cucumber: Factory Girl steps

This post is part of our Practical Cucumber series.

Did you know that factory_girl comes with some really useful cucumber steps You’re not alone, even though they’ve been around for a while.

Simply require them in your cucumber env.rb file:

require "factory_girl/step_definitions"

Then you can begin using them in your features:

Given a user exists with an email of "brandon@example.com"
And the following categories exist:
  | Name     |
  | Cucumber |
  | BDD      |
And the following article exists:
  | Title                       | Body   | Published at |
  | Factory Girl cucumber steps | lorem… | 2010-09-10   |

One of my favorite features of the factory_girl steps is that you can also specify associations:

Given the following user exists:
  | Name    | Email               |
  | Brandon | brandon@example.com |
And the following articles exist:
  | Title     | Author        |
  | Article 1 | Name: Brandon |
  | Article 2 | Name: Daniel  |

To make this to work, declare your associations in the factory and the steps will try to find or create by the association attribute.

Factory.define :article do |m|
  m.sequence(:title) {|i| "Article #{i}" }
  m.association :author, :factory => :user
end

Stop wasting time writing basic steps for creating models and spend your time working on the application.

This post is part of our Practical Cucumber series.

brandon@opensoul.org

Comments

Add a Comment

Hmm...that didn't work.

Something went wrong while adding your comment. If you don't mind, please try submitting it again.

Comment Added!

Your comment has been added to this post. Please refresh this page to view it.

Optional. If added, we will display a link to the website in your comment.
Optional. Never shared or displayed in your comment.
  1. September 09, 2010 at 12:34 PM

    In the words of Johnny Carson (through the voice of Dana Carvey), “I… I did not know that.”  But I’m very happy I do now; that’s awesome.

  2. September 09, 2010 at 16:50 PM

    I have been using Machinist lately and didn’t find any cucumber steps like these, so right away I hacked together some Machinist steps. http://ihswebdesign.com/blog/machinist-steps/

    Might be helpful to someone.

  3. dfugere@codegenome.com
    DAvid
    September 27, 2010 at 10:32 AM

    That really good to know. Thanks for this great tip!

  4. October 29, 2010 at 12:10 PM

    Thank you, I found this useful

  5. October 29, 2010 at 12:09 PM

    Thank you, I found this useful

  6. sgaviria@gmail.com
    neohomerico
    December 14, 2010 at 13:07 PM

    Thanks for sharing

  7. john.hinnegan@gmail.com
    John
    February 26, 2011 at 16:05 PM

    thanks

  8. dorian@supereveniment.ro
    Dorian
    May 18, 2011 at 6:44 AM

    That’s very interesting indeed. However i get the following error

    Undefined step: “the following user exists:” (Cucumber::Undefined exception)

    (i’ve installed all the required gems and updated the cucumbre env.rb file)

    Any hints on how to fix that? Thanks!

  9. May 18, 2011 at 6:59 AM

    Dorian, I know exactly what it is because I got stuck here a couple months ago, even though I’ve done this tons of times.

    Make sure you have a user factory defined. It isn’t enough to just have the model, you have to have the factory defined too. 

    Hope that solves it!

  10. alex_graven242@yahoo.com
    Al
    March 21, 2012 at 17:16 PM

    Is there a way to do this without tables?

  11. July 02, 2012 at 22:28 PM

    Alas, it appears these are to be removed in 4.0.

    http://robots.thoughtbot.com/post/25650434584/writing-better-cucumber-scenarios-or-why-were

  12. August 31, 2012 at 12:38 PM

    Ah, yes, these factory_girl steps have been removed. I wondered why I couldn’t get it to work. I should also remember check the date of when articles are written. =/

  13. August 31, 2012 at 12:39 PM

    Good point. Time for us to update the post!

  14. 13030142057@sicsr.ac.in
    Sibu Stephen
    January 07, 2015 at 0:35 AM

    The above steps really seemed to be ,helpful from the point of implementing Factory girl in Cucumber.

  15. September 21, 2015 at 15:48 PM

    It looks like they moved this somewhere, or got rid off it. I’m having trouble finding it now.