Using Factory Girl step definitions in Cucumber features (Rails 3)
Asked Answered
R

4

7

I'm trying to use Cucumber and Factory Girl. The following lines:

  Given I am not logged in
  And   the following user exists:
    | login  | email               | password   | confirmation |
    | user50 | [email protected] | secret50   | secret 50    | 
 ....

raises the following error:

Undefined step: "the following user exists:" (Cucumber::Undefined exception)
/home/user/RubymineProjects/project/features/sign_in.feature:9:in `And the following user exists:

You can implement step definitions for undefined steps with these snippets:
And /^the following user exists:$/ do |table|
  # table is a Cucumber::Ast::Table
  pending # express the regexp above with the code you wish you had
end

'

I've installed factory_girl_rails (even RubyMine's code completion feature works with Factory Girl step ... )

#Gemfile
group :test do
   gem "cucumber-rails", ">= 0.3.2"
   gem "factory_girl_rails"
 end

#features/support/env.rb
require 'factory_girl'
require 'factory_girl/step_definitions'

Any ideas? Thanks


Update: Thanks to @sj26 and @twmills I realized that I forgot to create a :user factory with Factory Girl. Once I created it, everything worked well.

Roofdeck answered 18/5, 2011 at 13:39 Comment(1)
Do you have a users.rb factory file?Eda
T
11

For those people, who trying to use FactoryGirl helpers now:

From FactoryGirl 3.5.0 these step helpers are deprecated and removed in 4.0.0: http://robots.thoughtbot.com/post/25650434584/writing-better-cucumber-scenarios-or-why-were

As of FactoryGirl 3.5.0, using any of FactoryGirl’s generated step definitions will print out a deprecation warning. We’ll be removing the step definitions completely in the 4.0.0 release of FactoryGirl in accordance with SemVer. I imagine the existing code will be extracted to a gem similar to Cucumber Rails’ training wheels with a nice warning urging developers not to use the the steps.

So if you want to use FactoryGirl in Cucumber you should use it in your own step definitions.

Tourmaline answered 7/11, 2012 at 10:29 Comment(1)
ok. if your answers gets at least 3 upvotes I will consider marking your answer as the right answer. (I do not use cucumber anymore so i cannot check)Roofdeck
T
7

You need to include your factories first. factory_girl/step_definitions will iterate over your defined factories to define a step for each.

I use this in features/support/factory_girl.rb:

# Require factories...
require 'spec/factories'

# Then define steps based on factories.
require 'factory_girl/step_definitions'
Telegonus answered 19/5, 2011 at 3:13 Comment(3)
I'm getting no such file to load -- spec/factories (LoadError) even though I have that folder and some factories I (successfully) use in rspec tests. I have also tried with require '../../spec/factories' but still no luck. As usual, RubyMine's code completion finds everything ... Very annoying, I would like to use the default steps provided by Factory Girl and not write my own..Roofdeck
Are you sure you have a factory :user? Can you gist your user model, factories file, cucumber support and cucumber feature?Telegonus
Thanks, that fixed the problem. I didn't have the :user factory. Sorry, it was a newbie question (I'm using Factory Girl for the first time)Roofdeck
A
1

Try this in features/step_definitions/user_steps.rb

Given /^the following user exists:$/ do |users|
  users.hashes.each do |user|
    Factory(:user,user)
  end
end

Although you may want this instead:

Given /^the following users:$/ do |users|
  etc..
Atmometer answered 18/5, 2011 at 16:34 Comment(4)
This duplicates the functionality of factory_girl's step_definitions, but WETly.Telegonus
Exactly, I would like to uses the factory_girl's step_definitions if possible. (If I can't, I will probably use this workaround). Thanks @Telegonus and @AtmometerRoofdeck
@Telegonus Old habits are hard to break. Factory Girl didn't include step definitions when I started using it. Need to make the switch.Atmometer
So how would you write the step definitions using the factory_girl's step_definitions ?Schober
E
1

Ok, I get that ThoughtBot wants us to write better code, but as an upgrade crutch, we can just put this old file in features/step_definitions:

https://raw.github.com/thoughtbot/factory_girl/3.6.x/lib/factory_girl/step_definitions.rb

Embus answered 14/1, 2013 at 5:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.