工厂女孩什么时候在数据库中创建对象?

发布于 2024-08-29 14:29:43 字数 1300 浏览 4 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦回旧景 2024-09-05 14:29:43

Factory(:user)Factory.create(:user) 的快捷方式,因此在您的设置中,您将创建两个对象并将它们保存到数据库中。

Factory.build(:user) 将为您创建一个 user 记录,而不将其保存到数据库中。

编辑

在您的 session_user 工厂中,您将创建一个用户,然后在测试设置中创建另一个用户。 FactoryGirl 将创建一个新的 user 记录,因为您在 session_user 工厂中有关联。

您可以从 session_user 对象获取您的 user 实例,如下所示:-

 context "normal user" do
  setup do
   session = Factory(:session_user)  
   @request.session[:session_id] = session.session_id
   @request.session[:user_id] = session.user_id
  end

或者您可以向 user 工厂添加一些详细信息以确保名称唯一和电子邮件地址如下:-

Factory.define :user do |u| 
 u.sequence(:login) {|n| "quentin#{n}" }
 u.sequence(:email) {|n| "quentin#{n}@example.com"}
end

Factory(:user) is a shortcut for Factory.create(:user) so within your setup you are creating two objects and saving them to the database.

Factory.build(:user) will create you a user 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 new user record because you have the association in the session_user factory.

You can either get your user instance from the session_user object as follows :-

 context "normal user" do
  setup do
   session = Factory(:session_user)  
   @request.session[:session_id] = session.session_id
   @request.session[:user_id] = session.user_id
  end

or you can add some details to the user factory to ensure unique name and email addresses as follows :-

Factory.define :user do |u| 
 u.sequence(:login) {|n| "quentin#{n}" }
 u.sequence(:email) {|n| "quentin#{n}@example.com"}
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文