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. ;-)

Jens Grassel Written by:

Solving problems and burning chrome for fun and profit.