Factory_Girl not generating unique records

2019-09-03 04:29发布

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

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.

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?

1条回答
我想做一个坏孩纸
2楼-- · 2019-09-03 05:04

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.

查看更多
登录 后发表回答