工厂女孩什么时候在数据库中创建对象?
我正在尝试使用 FactoryGirl
/shoulda
模拟会话(它可以使用固定装置,但我在使用工厂时遇到问题)。我有以下工厂(用户登录名和电子邮件都有唯一
验证):
Factory.define :user do |u|
u.login 'quentin'
u.email '[email protected]'
end
Factory.define :session_user, :class => Session do |ses|
ses.association :user, :factory => :user
ses.session_id 'session_user'
end
这是测试
class MessagesControllerTest < ActionController::TestCase
context "normal user" do
setup do
@request.session[:user_id]=Factory(:user).id
@request.session[:session_id]=Factory(:session_user).session_id
end
should "be able to access new message creation" do
get :new
assert_response :success
end
end
end
,但是当我运行rake test:functions
时,我得到这个测试结果,
1) Error:
test: normal user should be able to access new message creation. (MessagesControllerTest):
ActiveRecord::RecordInvalid: Validation failed: Account name already exists!, Email already exists!
这意味着当我在测试设置中引用它时,记录已经存在于数据库中。这里有什么我不明白的地方吗? FactoryGirl
是否在启动时在数据库中创建所有工厂?
Rails 2.3.5/shoulda/FactoryGirl
I am trying to simulate a session using FactoryGirl
/shoulda
(it worked with fixtures but i am having problems with using factories). I have following factories (user login and email both have unique
validations):
Factory.define :user do |u|
u.login 'quentin'
u.email '[email protected]'
end
Factory.define :session_user, :class => Session do |ses|
ses.association :user, :factory => :user
ses.session_id 'session_user'
end
and here's the test
class MessagesControllerTest < ActionController::TestCase
context "normal user" do
setup do
@request.session[:user_id]=Factory(:user).id
@request.session[:session_id]=Factory(:session_user).session_id
end
should "be able to access new message creation" do
get :new
assert_response :success
end
end
end
but when i run rake test:functionals
, I get this test result
1) Error:
test: normal user should be able to access new message creation. (MessagesControllerTest):
ActiveRecord::RecordInvalid: Validation failed: Account name already exists!, Email already exists!
which means that record already exists in db when I am referring to it in the test setup. Is there something I don't understand here? does FactoryGirl
create all factories in db on startup?
rails 2.3.5/shoulda/FactoryGirl
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Factory(:user)
是Factory.create(:user)
的快捷方式,因此在您的设置中,您将创建两个对象并将它们保存到数据库中。Factory.build(:user)
将为您创建一个user
记录,而不将其保存到数据库中。编辑
在您的
session_user
工厂中,您将创建一个用户,然后在测试设置中创建另一个用户。FactoryGirl
将创建一个新的user
记录,因为您在session_user
工厂中有关联。您可以从
session_user
对象获取您的user
实例,如下所示:-或者您可以向
user
工厂添加一些详细信息以确保名称唯一和电子邮件地址如下:-Factory(:user)
is a shortcut forFactory.create(:user)
so within your setup you are creating two objects and saving them to the database.Factory.build(:user)
will create you auser
record without saving it to the DB.EDIT
Within your
session_user
factory you are creating a user and then creating another within your test setup.FactoryGirl
will create a newuser
record because you have the association in thesession_user
factory.You can either get your
user
instance from thesession_user
object as follows :-or you can add some details to the
user
factory to ensure unique name and email addresses as follows :-