Dowling
Dowling

Reputation: 33

FactoryGirl + RSpec + Rails 3 Engine 'undefined method'

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?

Upvotes: 0

Views: 1530

Answers (2)

Dowling
Dowling

Reputation: 33

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.

Upvotes: 1

jimworm
jimworm

Reputation: 2741

An association is specified in factory_girl as association :factory_name.

Upvotes: 0

Related Questions