Skip to content


Factory girl breaks test migrations #88

retospect opened this Issue · 5 comments

2 participants


Factory girl sometimes breaks migrations in the test environment.
I found it's necessary to avoid the default naming scheme (spec/factories.rb etc), and add a "require 'spec/xfactory.rb'"line at the end of spec_helper.rb.

Otherwise, the migrations do not take place when I run rake db:test:prepare.

thoughtbot, inc. member

Can you include the version of Rails and factory_girl you're using, as well as the error you're seeing? It seems likely that this is a load order issue, so your factories file would be useful as well.


Rails 3.0.1
From Gemfile.lock

factory_girl (1.3.2)
factory_girl_rails (1.0)
  factory_girl (~> 1.3)
  rails (>= 3.0.0.beta4)

Factory file

From spec/factories.rb

Factory.define :user, :class => User do |usr|
usr.login 'wibble'

Factory.define :anonymous_client, :class => Client do |cl|
cl.authentication_token 'CAFE'


thoughtbot, inc. member

If you're using Rails 3, you probably want to check out factory_girl_rails. I'm not sure how your definitions are being auto-loaded for a Rails 2 app - do you require factory_girl or the step definitions manually somewhere?

My instinct is that if you change all the literal class references (ie User) to be strings referring to class names (ie "User") your errors will go away.


Changing the classes to strings did fix it. Is it possible to get an error message when literal class references are passed in, or is there a valid use case for that?

thoughtbot, inc. member

Passing in class references works fine outside of Rails, and it works fine in Rails if you control your load order. Because Rails autoloads missing constants, the load order is more or less implicit in most Rails applications.

@pjanik pjanik added a commit to concord-consortium/lara that referenced this issue
@pjanik pjanik Fix DB migrations (factory_girl can break it) bd3d3ca
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.