使用带有Rspec(Rails)的Factory girl时,不会创建测试数据库结构

时间:2013-01-08 12:38:38

标签: ruby-on-rails integration-testing factory-bot

我在rails应用程序中使用FactoryGirl而不是Fixtures。

当我尝试在我的测试中使用工厂女孩并创建一些测试数据时,它显示为

PG:Error relation "users" doesn't exists(我有一个名为User的模型)

但是当我运行rake db:test:clone,然后运行测试时,测试就会通过。 rake db:test:clone命令将所有表结构从开发db克隆到测试数据库,这解决了这个问题。

但在使用FactoryGirl时,我有什么方法可以不运行rake db:test:clone

或我错过了什么?

更新:

我发现了另外一件事,我有另一个使用Rspec和FactoryGirl的应用程序。在该应用程序中,在运行rake spec --trace命令

时执行以下操作
** Invoke spec (first_time)
** Invoke db:test:clone_structure (first_time)
** Invoke db:structure:dump (first_time)
** Invoke environment (first_time)
** Execute environment
** Invoke db:load_config (first_time)
** Execute db:load_config
** Execute db:structure:dump
** Invoke db:test:load_structure (first_time)
** Invoke db:test:purge (first_time)
** Invoke environment 
** Invoke db:load_config 
** Execute db:test:purge
** Execute db:test:load_structure
** Invoke db:structure:load (first_time)
** Invoke environment 
** Invoke db:load_config 
** Execute db:structure:load

但在任何新应用程序中,我都会在运行rake spec --trace

时看到以下内容已执行
** Invoke spec (first_time)
** Invoke noop (first_time)
** Execute noop
** Execute spec

请建议我缺少什么?

问候

巴兰

2 个答案:

答案 0 :(得分:1)

每次运行db:migrate时,也要运行db:test:prepare,因此数据库更改也会在您的测试数据库中进行镜像。

答案 1 :(得分:0)

我找到了解决方案,我们需要在customrake.rake下创建一个佣金任务/lib/tasks 并添加行task :spec => 'db:test:prepare',这将确保在运行Specs之前执行rake db:test:prepare