如何将 db:seed 数据自动加载到测试数据库中? [英] How to load db:seed data into test database automatically?

查看:47
本文介绍了如何将 db:seed 数据自动加载到测试数据库中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用在 Rails 2.3.4+ 中加载种子数据的新标准方式,即 db:seed rake 任务.

I'm attempting to use the new standard way of loading seed data in Rails 2.3.4+, the db:seed rake task.

我正在加载常量数据,这是我的应用程序真正正常运行所必需的.

I'm loading constant data, which is required for my application to really function correctly.

在测试之前运行 db:seed 任务的最佳方法是什么,以便预先填充数据?

What's the best way to get the db:seed task to run before the tests, so the data is pre-populated?

推荐答案

db:seed rake 任务主要只是加载 db/seeds.rb 脚本.因此只需执行该文件即可加载数据.

The db:seed rake task primarily just loads the db/seeds.rb script. Therefore just execute that file to load the data.

load "#{Rails.root}/db/seeds.rb"

# or

Rails.application.load_seed

放置位置取决于您使用的测试框架以及您是希望在每次测试之前加载它还是在开始时只加载一次.您可以将它放在 setup 调用或 test_helper.rb 文件中.

Where to place that depends on what testing framework you are using and whether you want it to be loaded before every test or just once at the beginning. You could put it in a setup call or in a test_helper.rb file.

这篇关于如何将 db:seed 数据自动加载到测试数据库中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆