Setup Factory Girl with Test::Unit and Shoulda

NolanDC picture NolanDC · Jul 21, 2009 · Viewed 14.4k times · Source

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 

Answer

Kenny picture Kenny · Aug 31, 2009

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.