Flaviu Simihaian's Blog - Entrepreneur and Developer

Factory_girl Association Validation Problems

You want to test if a foreign key exists (Question belongs_to Survey). Of course you use factory_girl.

You might approach it the way I have below, but then you get the error at the end, that makes absolutely no sense:

You might read that there is a difference between :create and :build and that’s the problem, so you change the survey association line to something like this:

But what’s really the problem?

Despite the rspec error, the problem is in the Survey factory definition, which has an after_build callback to generate a couple of questions every time it builds a survey:

Factory_girl does not like this recursiveness, and so you should take it out. Once you do, it all works.

If anyone has a better solution to this, please comment below.


Fork Flaviu on GitHub