[英]Test database structure is not created when using Factory girl with Rspec (Rails)
I am using FactoryGirl in my rails application instead of Fixtures. 我在Rails应用程序中使用FactoryGirl而不是Fixtures。
When i try to use factory girl in my test and create some test data, it shows like 当我尝试在测试中使用Factory girl并创建一些测试数据时,它显示为
PG:Error relation "users" doesn't exists
(i have a model named User) PG:Error relation "users" doesn't exists
(我有一个名为用户的模型)
But when i run rake db:test:clone
, and then run the test, the test is passed. 但是,当我运行rake db:test:clone
,然后运行测试时,测试通过了。 The rake db:test:clone
command clones all the table structure from development db to test db, and this fixes the issue. rake db:test:clone
命令可将所有表结构从开发db克隆到测试db,从而解决了该问题。
But is there any way for me to not to run rake db:test:clone
when using FactoryGirl? 但是有什么办法让我在使用FactoryGirl时不运行rake db:test:clone
吗?
or what am i missing? 还是我想念什么?
Update : 更新:
I found out one other thing, i have another application which uses Rspec and FactoryGirl. 我发现另一件事,我有另一个使用Rspec和FactoryGirl的应用程序。 In that application below are executed when running rake spec --trace
command 在该应用程序中,运行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
But in any of the new application i see the below executed when running rake spec --trace
但是在任何新应用程序中,运行rake spec --trace
时,我都会看到以下内容
** Invoke spec (first_time)
** Invoke noop (first_time)
** Execute noop
** Execute spec
Please suggest what am i missing? 请指出我想念的是什么?
regards 问候
Balan 巴兰
每次您运行db:migrate
, db:test:prepare
运行db:test:prepare
,因此数据库更改也将反映在您的测试数据库上。
我找到了解决方案,我们需要在/lib/tasks
下创建一个rake任务customrake.rake
并添加以下行task :spec => 'db:test:prepare'
,这将确保在执行rake db:test:prepare
之前运行规格。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.