0

I have been trying to use FactoryGirl while developing a Rails 3 mountable engine, and when the factory is called in the spec, it returns the following error:

NoMethodError: undefined method `foo_bar=' for #<Foo::Baz:0x007fed6e3e2700>

Models

Foo::Bar

module Foo
  class Bar < ActiveRecord::Base
    has_many :bazs
  end
end

Foo::Baz

module Foo
  class Baz < ActiveRecord::Base
    belongs_to :bar
  end
end

Factories

FactoryGirl.define do
  factory :foo_bar, class: Foo::Bar do
    ...
  end

  factory :foo_baz, class: Foo::Baz do
    foo_bar
  end
end

Looking at other topics FactoryGirl + RSpec + Rails 3 'undefined method =' and followed the answer and it still did not work.

Any ideas on how to solve this?

Community
  • 1
  • 1
Dowling
  • 33
  • 6

2 Answers2

1

The problem was caused by the calling create from FactoryGirl using the wrong association name in my spec.

FactoryGirl.create(:foo_baz, foo_bar: @bar, created_at: 1.day.ago)

With the help that jimworm provided, it pointed me to look at the spec, so I modified the call shown below.

FactoryGirl.create(:foo_baz, bar: @bar, created_at: 1.day.ago)

This fixed the issue.

Thank you, jimworm for your help.

Dowling
  • 33
  • 6
0

An association is specified in factory_girl as association :factory_name.

jimworm
  • 2,731
  • 18
  • 26
  • Tried `factory :foo_baz, class: Foo::Baz do association :foo_bar end` and it did not work. It gives the same error. – Dowling Sep 14 '12 at 23:16
  • Your association is not called `foo_bar`, but `bar`. Try `association :bar, factory: :foo_bar` – jimworm Sep 14 '12 at 23:31
  • I tried that and received the exact same error. It seems to me that FactoryGirl is having a problem with the engine's namespace, but being new to Rails and FactoryGirl, I could be wrong. – Dowling Sep 15 '12 at 13:32