FactoryGirl搞砸了rake

FactoryGirl搞砸了rake

本文介绍了FactoryGirl搞砸了rake db:迁移过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Ruby on Rails 3中使用Rspec(2.11.0)和FactoryGirl(4.0.0)进行TDD/BDD.我有一个用于Category模型的工厂:

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

如果删除,请在 test 环境中创建然后迁移数据库,我会收到此错误:

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

rake aborted!
Could not find table 'categories'

发生此问题的原因是FactoryGirl希望表已经存在(出于某种奇怪的原因).如果我从rails应用程序中删除spec文件夹并执行db:migrate,它将起作用.同样,如果我将我的Gemfile中的factory-girl-rails标记为:require => false,它也可以工作(然后我必须注释要求才能运行rspec).

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).

我在这里找到了有关此问题的一些信息: https://github.com/thoughtbot/factory_girl/issues/88

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

我在做错什么吗?如何在db:migration任务中跳过" FactoryGirl阶段?

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

推荐答案

我认为您需要像Gemfile中那样具有工厂女孩定义:

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

  gem 'factory_girl_rails', :require => false

然后您只需要在spec_helper.rb中添加它,

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

  require 'factory_girl_rails'

这就是我一直使用此gem的方式.您不需要在spec_helper.rb之外的其他地方要求它.您当前所需的方法是错误的.

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.

这篇关于FactoryGirl搞砸了rake db:迁移过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 04:31