Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 368
Dorian Avatar asked May 18 '11 13:05

Dorian


2 Answers

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.

like image 127
Mik Avatar answered May 31 '23 17:05

Mik


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'
like image 44
sj26 Avatar answered May 31 '23 16:05

sj26