cmcdaniels
cmcdaniels

Reputation: 145

Using FactoryGirl in Rails Engine

I've been having trouble accessing my FactoryGirl definitions within the rails console of the spec/dummy scope. I understand that as my definitions are in the spec/factories folder they would not be accessible to the console, but how would I go about including or finding these definitions for in it.

The namespace of FactoryGirl is defined within both the test and development console environment, however the factories I have in factories.rb in spec/factories are not registered.

My setup is from this tutorial: http://viget.com/extend/rails-engine-testing-with-rspec-capybara-and-factorygirl

Upvotes: 1

Views: 1002

Answers (1)

guero64
guero64

Reputation: 1049

I was getting an error like this for the longest time, no matter what stackoverflow suggestion I tried:

ArgumentError: Factory not registered: item

I finally realized that when my factories were generated, my engine's name was being prefixed in the factory file, so that instead of:

FactoryGirl.define do
  factory :item do
    ...
  end
end

I was seeing:

FactoryGirl.define do
  factory :my_engine_item, :class => 'MyEngine::Item' do
    ...
  end
end

In the end to get things to work I simply referred to my_engine_item instead of item in my FactoryGirl declarations, so that:

FactoryGirl.build(:item)

becomes

FactoryGirl.build(:my_engine_item)

It's not a very elegant solution and maybe not even the solution to your problem, but I thought I would share what I went through for whatever it's worth.

Upvotes: 1

Related Questions