使用 RSpec,如何在加载时为数据库设置种子? [英] With RSpec, how to seed the database on load?

查看:37
本文介绍了使用 RSpec,如何在加载时为数据库设置种子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 rspec 来测试我的 rails 3 应用程序.我需要在测试开始之前为数据库做种.如何使用以下内容为数据库设定种子:

I'm using rspec for testing w my rails 3 app. I need to seed the database before the tests start. How can I seed the database with the following:

/db/seeds.rb

["Admin", "Member"].each do |role_name|
  Role.find_or_create_by_name(role_name)
end

谢谢

推荐答案

试试,像这样

rake db:seed RAILS_ENV=test

您可以获得所有 rake 命令的列表

You can get a list of all rake commands doing

rake -T

如果这是测试数据,您可能需要考虑将其放入将在测试开始时加载的夹具中.

If this is test data, you may want to look at putting it into fixtures which will be loaded on the start of the tests.

这篇关于使用 RSpec,如何在加载时为数据库设置种子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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