FactoryGirl screws up rake db:migrate process

Ilea Cristian picture Ilea Cristian · Sep 14, 2012 · Viewed 8.9k times · Source

I am doing TDD/BDD in Ruby on Rails 3 with Rspec (2.11.0) and FactoryGirl (4.0.0). I have a factory for a Category model:

FactoryGirl.define "Category" do
  factory :category do
    name "Foo"
  end
end

If I drop, create then migrate the database in the test enviroment I get this error:

rake aborted!
Could not find table 'categories'

This problem occurs because FactoryGirl expects the tables to already exist (for some odd reason). If I remove the spec folder from my rails app and do db:migrate, it works. Also if I mark factory-girl-rails from my Gemfile as :require => false it also works (then I have to comment that require in order to run rspec).

I found some information about this problem here: https://github.com/thoughtbot/factory_girl/issues/88

Is there something wrong that I'm doing? How can I "pass by" the FactoryGirl stage in the db:migration task?

Answer

Vadim Chumel picture Vadim Chumel · Sep 14, 2012

I think you need to have factory girl definition like that in Gemfile:

  gem 'factory_girl_rails', :require => false

And then you just require it in your spec_helper.rb like that:

  require 'factory_girl_rails'

This is the way I'm always using this gem. You don't need to require it in other places than spec_helper.rb. Your current desired approach is just wrong.