Factory_Girl未生成唯一记录 [英] Factory_Girl not generating unique records
问题描述
我正在将我的应用程序转换为使用工厂,而不是使用Factory_Girl_Rails的固定装置.我定义了以下工厂:
I'm converting my application over to use factories instead of fixtures with Factory_Girl_Rails. I have the following factory defined:
factory :requirement do
sequence(:reqTitle) {|t| "Test Requirement #{t}"}
ignore do
categoryName " "
categoryAbbr " "
end
reqText "This is a test general requirement for the purpose of, um, testing things"
status "Approved"
factory :reqWithCat do
category
end
factory :reqWithNamedCat do
category {create(:category, catName: categoryName, catAbbr: categoryAbbr)}
end
factory :reqFromUserRequirement do
user_requirement
end
end
然后,在设置"部分中,运行以下代码段:
Then, in the setup section, I run the following snippet:
(0..5).each do |x|
requirement = create(:reqWithCat)
requirement.ind_requirements {|ir| [create(:ind_requirements)]}
end
(0..5).each do |x|
create(:reqWithNamedCat, categoryName: "User Interface", categoryAbbr: "UI")
end
但是,我的测试失败了,这显然是因为未创建记录(例如,需求控制器上的索引测试告诉我,当应该有10条记录时,返回了0条记录).我在调试模式下运行测试,发现创建的每个需求都具有完全相同的id值.此外,每条记录具有相同的序列值.
However, my tests are failing, apparently because records aren't being created (for instance, the index test on the requirements controller tells me that there 0 records returned when there should be 10). I run the tests in debug mode, and discover that every requirement created has the exact same id value. Also, each record has the same sequence value.
我认为重复记录无法保存,这就是为什么我得到0回报的原因.但是,我看不到设置不正确的内容.我在这里想念什么?
I believe the duplicate records are failing to save, which is why I'm getting a 0 return. However, I can't see what I've set up incorrectly. What am I missing here?
推荐答案
一旦修复了用户工厂以正确创建相关工厂后,我发现我根据定义范围的方式错误地编写了该工厂.楷模.解决该问题后,中断"测试就开始起作用.
Once I fixed the user factories to create the related many properly, I then discovered that I had written this factory incorrectly based on the way I was defining scopes in my models. Once that was fixed, the "broken" test began working.
这篇关于Factory_Girl未生成唯一记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!