Setup Factory Girl with Test::Unit and Shoulda
Asked Answered
A

13

16

I'm trying to set up Factory Girl with Test::Unit and Shoulda in Ruby on Rails. I have installed the gem, created my factory file under the test/factories directory, and created my spec file under the test/models directory. The current error I'm getting is 'ArgumentError: No such factory: test', which leads me to believe that the test_factory.rb file is not being loaded? Any idea as to what I should change?

Here are my files.

#test/factories/test_factory.rb
Factory.define :test do |t|  
  t.name 'test_spotlight'  
  t.label 'test spotlight label'  
end

and

#test/modes/test_spec.rb
require 'test_helper'
require 'factory_girl'
class TestTest < Test::Unit::TestCase
  def setup
    @test = Factory.build(:test)
  end

  context "A test" do
    should "save with the minimum requirements" do
      assert @test.save
    end
  end 
end 
Amor answered 21/7, 2009 at 15:38 Comment(0)
U
40

I've run into this problem on one of my projects too. I'm not sure precisely what's causing the initialization code to be skipped but you can force load the factory definitions like this:

require 'factory_girl'
Factory.find_definitions

Hope this helps.

Uriisa answered 31/8, 2009 at 20:8 Comment(3)
This worked for me by placing it in my test_helper.rb file. I ran into this while using factory_girl 1.3.3, rails 2.3.11, ruby 1.8.7, and bundler 1.0.10Ioannina
Worked for me rails 3.0.9 while doing railstutorial. Placed in my user_helper file.Sneakbox
For Rails 3.04, FactoryGirl 3.3.0, use: FactoryGirl.find_definitions, as opposed to Factory.find_definitionsSulcus
C
3

Try putting this in test_helper.rb:

require 'factory_girl'
Dir.glob(File.dirname(__FILE__) + "/factories/*").each do |factory|
  require factory
end
Collectivize answered 23/11, 2009 at 20:53 Comment(0)
R
3

Just discovered factory_girl_rails, where it says the autoloading is the only extra thing it has https://github.com/thoughtbot/factory_girl_rails

Rella answered 1/4, 2011 at 10:37 Comment(0)
I
2

I had the same problem. Eventually I made do by putting all my factories in "/test/factories.rb" and writing the following lines in my "/test/test_helper.rb" file:

require 'factory_girl'
require File.dirname(__FILE__) + "/factories"

you could do the same thing for multiple files by requiring them in the test_helper. I haven't yet figured out why the auto-include that's mentioned in "factory_girl's" readme doesn't happen.

Inocenciainoculable answered 12/11, 2009 at 17:24 Comment(0)
F
2

I also managed to get rid of this issue by putting this line into my environment.rb:

config.gem "factory_girl", :source => "http://gemcutter.org"

Make also sure you have the latest gem:

Name changed from "thoughtbot-factory_girl" to "factory_girl", source changed from "http://gems.github.com" to "http://gemcutter.org".

Fascist answered 18/12, 2009 at 9:59 Comment(0)
C
2

In case you had this issue with ruby 1.9.2, require expects the expanded path.

File.expand_path("test/factories.rb")

This patch solved my problem. I just sent a pull request. After that you can add this to your test_helper.rb:

require 'factory_girl'
FactoryGirl.find_definitions
Casting answered 24/7, 2010 at 15:6 Comment(0)
R
1

If I only required 'factory_girl' in test_helper.rb, I would get the same behavior you mentioned, yet if I required it in my config/test/environment.rb (note I use environmentalist) it would properly find the Factory definition without any issue.

I tried this after reading the factory girl rdoc where it says to put config.gem in your environment.

Rage answered 22/11, 2009 at 22:24 Comment(0)
F
1

I also ran into the problem - after updating FactoryGirl to 1.3.2 - that factories from test/factories were not loaded automatically any more.

I could get rid of the problem by adding the code from dg into test_helper.rb:

Dir.glob(File.dirname(__FILE__) + "/factories/*.rb").each do |factory|
 require factory
end

When running single tests in Textmate, everything worked fine, but running e.g. all unit tests from the command line using rake test:units failed with a DuplicateDefinitionError (I read that it has probably something to do with ruby 1.8.x). So I slightly changed the code:

if (!Factory.factories || Factory.factories.empty?)
  Dir.glob(File.dirname(__FILE__) + "/factories/*.rb").each do |factory|
    require factory
  end
end
Fascist answered 19/10, 2010 at 13:24 Comment(0)
S
0

Have you tried moving the

require 'factory_girl'

to your test/test_helper.rb ?

The factory auto-loading mechanism may depend on where the the require is called. It could be trying to find factories *test/models/factories/** instead of *test/factories/**

State answered 21/7, 2009 at 21:1 Comment(0)
E
0

Instead of naming your factory file test_factory.rb, try naming it factory.rb

Empirin answered 22/7, 2009 at 3:3 Comment(1)
the docs say that any .rb file under the test/factories directory should get loadedGymnast
S
0

Interesting. I had a similar problem trying to get cucumber to work with factory_girl. I had originally configured factory_girl to be looked for ('config.gem') but not loaded in the cucumber environment and fully required in 'features/support/env.rb', same as cucumber does for webrat, etc. That only started to work when I explicitly told factory_girl to find its definitions as Kenny suggested above.

When I removed the require statement from env.rb and fully required factory_girl in the cucumber environment, the effect went away and factory_girl worked out of the box.

So it really seems to be a question of when (or in which context) factory_girl gets loaded.

Seasonseasonable answered 9/3, 2010 at 12:9 Comment(0)
S
0

Since different people are using different versions of Rails (2.x and 3.x being the most common now), it is important to include the other pertinent pieces of your environment (the most important being which version of Rails you're on). From the factory_girl web page, version 1.3.0 documentation (http://rubydoc.info/gems/factory_girl/1.3.0/frames):

If you want to use factory_girl with Rails 3, use the factory_girl_rails gem, not this one.

If you want to use factory_girl with Rails versions prior to Rails 3, use version 1.2.4.

If you're having trouble with the loading, I'd suggest making sure that you are using the right version. The versions of factory_girl greater than 1.2.4, I'd assume, are brought in as dependencies for the 'factory_girl_rails' (Rails 3.0+) gem.

Superfuse answered 24/8, 2011 at 21:52 Comment(0)
B
0

I added require 'factory_girl' require File.dirname(FILE) + "/factories" to spec_helper.rb whiche helped, but then I rememberd Spork can sometimes ba a bit of a problem, so I restarted Spork without the require and then it worked fine.

Batik answered 19/9, 2011 at 14:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.