Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FactoryGirl screws up rake db:migrate process

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?

like image 575
Ilea Cristian Avatar asked Sep 14 '12 11:09

Ilea Cristian


People also ask

How does rake db migrate work?

A migration means that you move from the current version to a newer version (as is said in the first answer). Using rake db:migrate you can apply any new changes to your schema. But if you want to rollback to a previous migration you can use rake db:rollback to nullify your new changes if they are incorrectly defined.

How do I undo a rake database?

just use rake db:reset , that will drop your database (same as undoing all migrations) and reset to the last schema. UPDATE: a more correct approach will be using rake db:migrate:reset . That will drop the database, create it again and run all the migrations, instead of resetting to the latest schema.

What does rake db schema load do?

Unlike rake db:migrate that runs migrations that have not run yet, rake db:schema:load loads the schema that is already generated in db/schema. rb into the database. Always use this command when: You run the application for the first time.

What is rake database?

Rake is a utility built into Ruby and Rails that provides an efficient way to manage database changes. You can easily migrate database changes to servers by only using a command line!


2 Answers

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.

like image 118
Vadim Chumel Avatar answered Sep 20 '22 19:09

Vadim Chumel


A simple fix to this issue is to delay evaluation of any models in your factories by wrapping them in blocks. So, instead of this:

factory :cake do   name "Delicious Cake"   frosting Frosting.new(:flavor => 'chocolate')   filling Filling.new(:flavor => 'red velvet') end 

Do this (notice the curly braces):

factory :cake do   name "Delicious Cake in a box"   frosting { Frosting.new(:flavor => 'chocolate') }   filling { Filling.new(:flavor => 'red velvet') } end 

If you have a lot of factories this may not be feasible, but it is rather straightforward. See also here.

like image 37
polm23 Avatar answered Sep 21 '22 19:09

polm23