简体   繁体   English

如何在Rails中的预填充数据库上运行测试?

[英]How can I run tests on a pre-populated database in Rails?

So I am writing acceptance tests for a single feature of a large App. 因此,我正在为大型应用程序的单个功能编写验收测试。 I needed a lot of data for this and have a lot of scenarios to test; 为此,我需要大量数据,并且需要测试许多场景。 so I have pre-populated a mysql database using Sequel Pro. 因此,我已经使用Sequel Pro预填充了mysql数据库。

Obviously the appname_test database is in place for the other tests in the app. 显然,应用程序中的其他测试已使用appname_test数据库。 I would like to know how I could load up a .sql file (which is a sql dump of content) into this database at the start of my tests (before the first context statement). 我想知道如何在测试开始时(在第一个context语句之前)将.sql文件(这是内容的sql转储)加载到此数据库中。

I am new to development so this is completely new to me. 我是开发的新手,所以这对我来说是全新的。 Thanks in advance for any help. 在此先感谢您的帮助。

Update: 更新:

I have used the yaml_db gem to dump the dev db ( db:data:dump ) and then load it into the test db ( db:data:load ENV_RAILS=test ). 我已经使用yaml_db gem来转储dev db( db:data:dump ),然后将其加载到测试db( db:data:load ENV_RAILS=test )。 However, as soon as I run my specs the test db is wiped clean! 但是,一旦我运行我的规格,测试数据库就会被清除干净! Is there a way to run db:data:load ENV_RAILS=test from inside the spec file. 有没有一种方法可以从规范文件中运行db:data:load ENV_RAILS=test I have tried: 我努力了:

require 'rake'
Rake::Task['bundle exec db:data:load ENV_RAILS=test'].invoke

but it says Don't know how to build task 'be db:data:load ENV_RAILS=test' 但它说不Don't know how to build task 'be db:data:load ENV_RAILS=test'

OK, so here is what I did to solve this. 好的,这就是我要解决的问题。

I used the yaml_db gem and rake db:data:dump which creates db/data.yml (a dump of the dev db). 我使用了yaml_db gem和rake db:data:dump来创建db / data.yml(开发db的转储)。

I then had to write a library and rake task which converted the data.yml file into individual fixtures files. 然后,我不得不编写一个库和rake任务,将data.yml文件转换为单独的夹具文件。 I run this new rake task once to create the fixure files. 我运行此新的rake任务一次以创建固定文件。

Then, at the start of my spec I call fixtures :all which populates the test database with all the fixtures. 然后,在我的规范开始时,我称为fixtures :all ,它使用所有的fixtures填充测试数据库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将新列或表添加到资源中预先填充的sqlite数据库并使用SQLiteAssetHelper - How to add a new column or table to sqlite database pre-populated in the assets and using SQLiteAssetHelper 使用预填充表中的 Laravel Faker 填充数据库表 - Populate database table with Laravel Faker from pre-populated table 使用PHP中的预填充下拉列表将输出发送到INSERT - Using pre-populated drop down list in PHP to send output to INSERT MySQL 用预填充数据的表中的数据填充多个未使用的行 - MySQL Fill multiple unused rows with data in a table with pre-populated data 使用预先填充的表单更新数据库中的记录 - Updating records in database using a pre populated form 使用ajax和jquery从数据库动态添加预填充的表单 - Dynamically add pre populated forms from database using ajax and jquery 我的应用程序访问远程数据库。 如何有效运行单元测试? - My app accesses a remote database. How do I run unit tests efficiently? 如何在Play框架中运行依赖于MySQL数据库的测试? - How do I run tests in Play Framework that depend on having a mysql database? 如何运行插入查询脚本(.sql)创建表和列或预填充一些数据 - How can i run a insert query script(.sql) to create tables and columns or pre populate few datas 如何在rails上的ruby中复制mySQL数据库? - How can I copy a mySQL Database in ruby on rails?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM