FactoryGirl definition - undefined method to combine

I have the following

in / app / models:

class Area < ActiveRecord::Base has_many :locations end class Location < ActiveRecord::Base belongs_to :area end 

in / app / test / factories / areas.rb

 FactoryGirl.define do factory :area do name 'Greater Chicago Area' short_name 'Chicago' latitude 42 longitude -88 end factory :losangeles, class: Area do name 'Los_Angeles Area' short_name 'Los Angeles' latitude 50 longitude 90 end end 

in / app / test / factories / locations.rb

 FactoryGirl.define do factory :location do name "Oak Lawn" latitude 34 longitude 35 association :area end factory :malibu, class: Location do name "Malibu" latitude 60 longitude -40 association :losangeles end end 

When I try to run this, I get:

 NoMethodError: undefined method `losangeles=' for #<Location:0x00000102de1478> test/unit/venue_test.rb:10:in `block in <class:VenueTest>' 

Any help was appreciated.

+6
source share
1 answer

You get this error because you are trying to tell your malibu factory to establish an association with the name losangeles that does not exist. There is a factory losangeles that creates Area . Do you want to:

 FactoryGirl.define do factory :location do name "Oak Lawn" latitude 34 longitude 35 association :area end factory :malibu, class: Location do name "Malibu" latitude 60 longitude -40 association :area, factory: :losangeles end end 

See documentation here

Note that you can also use nesting to define a second factory:

 FactoryGirl.define do factory :location do name "Oak Lawn" latitude 34 longitude 35 association :area factory :malibu do name "Malibu" latitude 60 longitude -40 association :area, factory: :losangeles end end end 
+9
source

All Articles