Posted about 7 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:

Copy
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:

Avatar
jan0sch
Last edit:
about 4 years ago
by jan0sch
Posted by jan0sch to jan0sch's deck
This website uses cookies to improve usability and analyze traffic.
Accept or learn more