Bill Kocik
unread,Jul 2, 2009, 11:57:08 AM7/2/09Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to factory_girl
I have these relationships in my models:
class User < ActiveRecord::Base
has_many :groups, :include => [:group_memberships], :dependent
=> :destroy
belongs_to :default_group, :class_name => 'Group'
....
end
class Group < ActiveRecord::Base
belongs_to :user
...
end
My group test needs to ensure that when a test group is created, it
has a parent user. Likewise, when a test user is created, it must have
a set of groups, one of which is the default. So I have these in my
factories.rb:
Factory.define :user, :default_strategy => :build do |f|
...
f.groups {|groups| [groups.association(:group)]}
f.default_group {|d| d.groups.first}
end
Factory.define :group, :default_strategy => :build do |f|
f.sequence(:name) {|n| "Test Group #{n}"}
f.association :user
end
The problem is, if I turn off either the f.association :user line in
my group factory, or the f.groups line in my user factory, then my
tests for that model fail because the association isn't put together.
But with both of those enabled, then my tests for either model go into
infinite loops.
What am I doing wrong?