简体   繁体   English

将Factory girl与Rspec(Rails)结合使用时,不会创建测试数据库结构

[英]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:migratedb: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.

相关问题 在RSpec中使用factory_bot(以前为factory_girl)在Rails中进行测试时,未定义的方法“ first” - When using factory_bot (formerly factory_girl) with RSpec to test in Rails, undefined method “first” 使用工厂女孩与Rspec - Using Factory Girl with Rspec 创建记录似乎不会持久存储到测试数据库rspec,factory_girl_rails - Creating a record does not appear to persist to the test database, rspec, factory_girl_rails 如何使用RSpec集成测试和Factory Girl测试Rails的归属关系 - How to test Rails belongs_to relationship using rspec integration test and factory girl 尝试与Factory Girl一起运行Rspec时测试失败 - Test failing when trying to run Rspec with Factory Girl 使用rspec和factory girl错误查看测试:无路由匹配 - view test using rspec & factory girl error: No route matches 我想使用工厂女孩针对特定用例编写rspec测试 - I want to write rspec test using factory girl for a particular usecase 如何使用带有工厂女孩的rspec在Rails中进行测试? - How to do testing in rails using rspec with factory girl? 如何在Ruby on Rails中使用Rspec Capybara和Factory Girl计算循环 - How to count a loop using Rspec Capybara & Factory Girl in Ruby on rails 将guard-rspec与Factory-girl-rails一起使用 - Using guard-rspec with factory-girl-rails
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM