Factory.next在FactoryGirl 4.x和Rails 3.0中不起作用。谁知道更换?

时间:2022-03-29 23:25:23

I'm very new to Rails and am following along in the Ruby on Rails 3 Tutorial book by Michael Hartl and am running into a little bump while using the factory_girl gem.

我是Rails的新手,并且正在跟随Michael Hartl的Ruby on Rails 3 Tutorial一书,并且在使用factory_girl gem时遇到了一些小问题。

Specifically, I'm not sure how to update the code

具体来说,我不知道如何更新代码

Factory.next(...)

Before coming to this, I did run into a little problem between the older version of FactoryGirl used in the book and the current 4.1 version I'm using now, but was able to resolve it.

在此之前,我确实遇到了本书中使用的旧版FactoryGirl和我现在使用的当前4.1版本之间的一个小问题,但是能够解决它。

Specifically, the old way of writing code as

具体来说,旧编写代码的方式为

user = Factory(:user)

user =工厂(:用户)

needed to be updated to

需要更新到

user = FactoryGirl.create(:user)

user = FactoryGirl.create(:user)

That was fine, but now I'm coming to the code (as written in the book):

那很好,但是现在我要来代码了(如书中所写):

spec/controllers/users_controler_spec.rb

.    
@users << Factory(:user, :email => Factory.next(:email))
.

which I've tried updating to

我试过更新到

.
@users << FactoryGirl.create(:user, :email => FactoryGirl.next(:email))
.

but get the error:

但得到错误:

Failure/Error: @users << FactoryGirl.create(:user, :email => FactoryGirl.next(:email))
 NoMethodError:
   undefined method `next' for FactoryGirl:Module

I've tried a few different variations but still can't quite get it. Is the problem I'm having with FactoryGirl and just not using the gem correctly or does it have something to do with the Ruby methods?

我尝试了一些不同的变化,但仍然无法得到它。我与FactoryGirl有问题,只是没有正确使用gem或者它与Ruby方法有什么关系?

1 个解决方案

#1


3  

It looks like you have to use the method generate for sequences:

看起来您必须使用方法生成序列:

@users << Factory(:user, :email => FactoryGirl.generate(:email))

See the docs on sequences and associations for details.

有关详细信息,请参阅序列和关联的文档。

#1


3  

It looks like you have to use the method generate for sequences:

看起来您必须使用方法生成序列:

@users << Factory(:user, :email => FactoryGirl.generate(:email))

See the docs on sequences and associations for details.

有关详细信息,请参阅序列和关联的文档。