0

I am trying to nest an object inside another using FactoryGirl. I have this

# app/models/one.rb
class One < ActiveRecord::Base
    has_many :two_and_three
end

And the second model

# app/models/two_and_three.rb
class TwoAndThree < ActiveRecord::Base
  attr_accessible :cast
  belongs_to :one
end

I am trying to mock them like this:

# spec/factories/one.rb
FactoryGirl.define do
  factory :one do
    two_and_three FactoryGirl.build(:TwoAndThree)
  end
end

And

# spec/factories/two_and_three.rb
FactoryGirl.define do
  factory :TwoAndThree do
    cast Faker::Name.name
  end
end

I am getting uninitialized constant TwoAndThree when I run the test. How do I make FactoryGirl mock an object inside a mocked object?

1
  • Is your second code snippet correct? It shows a class name of One instead of TwoAndThree. Mar 7, 2014 at 4:45

1 Answer 1

0

Your class definition in two_and_three.rb is incorrect. It has One instead of TwoAndThree, which is why you're getting an uninitialized constant error. It's auto-loading the file, but the class is not being defined as a result.

2
  • It's actually TwoAndThree. Copy paste error on my part, sorry about that. Also, the error is from two_and_three FactoryGirl.build(:TwoAndThree) in one.rb, according to the stack trace.
    – ACC
    Mar 7, 2014 at 6:53
  • @AbhishekChanda If you're getting that uninitialized constant error and your model name is correct, then the only thing I can think of is that you haven't required spec_helper and therefore haven't enabled auto loading. Mar 7, 2014 at 7:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.