2011-05-18 73 views
7

我正在嘗試使用黃瓜和工廠女孩。下面的幾行:在Cucumber特性中使用Factory Girl步驟定義(Rails 3)

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

引發以下錯誤:

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 

'

我已經安裝了factory_girl_rails(甚至RubyMine的代碼完成功能與工廠女孩步的作品...)

#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' 

任何想法?謝謝


更新:感謝@ sj26和@twmills我意識到我忘了用Factory Girl創建一個:用戶工廠。一旦我創建它,一切運作良好。

+1

你有users.rb的工廠文件? – twmills 2011-05-18 15:51:50

回答

11

對於這些人,誰試圖現在使用FactoryGirl助手:

從FactoryGirl 3.5.0這些步驟助手已被取消,刪除在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.

所以,如果你想在黃瓜使用FactoryGirl你應該在你自己的步驟定義使用它。

+0

好的。如果您的答案至少有3個贊成票,我會考慮將您的答案標記爲正確的答案。 (我不再使用黃瓜,所以我不能檢查) – Dorian 2012-11-09 09:42:08

+0

@Dorian它已經有 – 2013-02-03 09:19:12

+0

好的。完成了!感謝您的跟進。 – Dorian 2013-02-04 09:40:03

1

在功能/ step_definitions/user_steps.rb

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

試試這個雖然你可能想這個代替:

Given /^the following users:$/ do |users| 
    etc.. 
+0

這複製了factory_girl的step_definitions的功能,但WETly。 – sj26 2011-05-19 03:19:00

+0

的確如此,如果可能的話,我想使用factory_girl的step_definitions。 (如果我不行,我可能會使用此解決方法)。謝謝@ sj26和@MacksMind – Dorian 2011-05-19 09:58:12

+0

@ sj26舊習慣很難打破。 Factory Girl在我開始使用時沒有包含步驟定義。需要開關。 – MacksMind 2011-05-19 18:47:03

7

您需要首先包括你的工廠。 factory_girl/step_definitions將遍歷您定義的工廠,爲每個工廠定義一個步驟。

features/support/factory_girl.rb使用:

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

# Then define steps based on factories. 
require 'factory_girl/step_definitions' 
+0

即使我有那個文件夾和一些工廠,我(成功)在rspec測試中使用,我得到'沒有這樣的文件來加載規格/工廠(LoadError)'。我也嘗試過'require'../../ spec/factories'',但仍然沒有運氣。像往常一樣,RubyMine的代碼完成找到了一切...非常討厭,我想使用Factory Girl提供的默認步驟,而不是自己寫.. – Dorian 2011-05-19 09:52:02

+0

你確定你有一個'factory:user'嗎? 你可以[gist](https://gist.github。com)你的用戶模型,工廠文件,黃瓜支持和黃瓜功能? – sj26 2011-05-21 03:50:23

+0

謝謝,解決了這個問題。我沒有:用戶工廠。對不起,這是一個新手問題(我第一次使用Factory Girl) – Dorian 2011-05-24 21:05:58

相關問題