FactoryGirl:构建对象将创建其关联的对象 [英] FactoryGirl: Building an object creates its associated object

查看:50
本文介绍了FactoryGirl:构建对象将创建其关联的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道这是FactoryGirl的错误还是我做错了

I don't know if this is a bug with FactoryGirl or if it is something i am doing wrong

我有两个工厂定义

factory :employee do
  name "name1"
  association :department
end

factory :department do
  name "department1"
end

我希望以下内容可以同时建立员工和部门

I would expect the following to build both employee and department

FactoryGirl.build(:employee, :name => "employee")

但是它将建立雇员对象并在数据库中创建部门.我确信它可以在某些旧版本的FactoryGirl中使用.

But it builds the employee object and creates department in the database. I am sure it use to work in some older versions of FactoryGirl.

我正在使用factory_girl 4.2.0版.

I am using factory_girl version 4.2.0.

我如何使它构建关联的对象而不是创建一个?

How do i make it build the associated objects instead of creating one?

推荐答案

您可以使用build_stubbed

FactoryGirl.build_stubbed :employee

然后,FactoryGirl将在内存中构建一个雇员对象和一个部门对象.两者都有伪造的ID(例如1000+),并且已正确关联.

Then FactoryGirl will build an employee object and a department object in memory. All of the two have fake ids like 1000+, and correctly associated.

这篇关于FactoryGirl:构建对象将创建其关联的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆