Posted about 8 years ago. Visible to the public.

Rails, FactoryGirl and has_and_belongs_to_many

When testing has_and_belongs_to_many models using rspec and factory girl you should setup your factory the following way:

FactoryGirl.define do factory :foo do name "Foo" end factory :bar do name "Bar" foos { |a| [a.association(:foo)] } end end

This is slightly more verbose than association(:foos, :factory => :foo) but it works without getting errors. ;-)

Owner of this card:

Last edit:
almost 5 years ago
by jan0sch
Posted by jan0sch to jan0sch's deck
This website uses short-lived cookies to improve usability.
Accept or learn more