We use thoughtbot’s FactoryGirl extensively in our daily spec (& feature) writing. One recent problem we encounter is the failure to correctly set the associations between the following 2 example models.
1 2 3 4 5 6 7 |
|
The factory definitions are as follow:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
The problem with the above definitions is that calling
Factory(:commented_blog)
creates 2 blogs & 1 comment
instead of the
expected 1 blog & 1 comment
, and the comment isn’t owned by the
intended blog.
Fixing this problem turns up to be pretty simple, all we need to do is when creating a comment, we enforces the desired ownership:
1 2 3 4 5 6 7 8 9 10 |
|
If you have some time to spare, u really should take a good read of this doc