如何在不运行rake spec的情况下为Rails rspec测试准备测试数据库? [英] How do I prepare test database(s) for Rails rspec tests without running rake spec?

查看:107
本文介绍了如何在不运行rake spec的情况下为Rails rspec测试准备测试数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

经过大量故障排除后,我发现我需要先运行rake spec(我可以使用control-c中止),然后才能直接运行rspec(例如,在我们的部分规格上).我们正在运行Rails 3.0.7和RSpec 2.5.0.

After significant troubleshooting, I figured out that I needed to run rake spec once (I can abort with control-c) before I can run rspec directly (e.g. on a subset of our specs). We are running Rails 3.0.7 and RSpec 2.5.0.

很明显,rake正在运行一些重要的数据库设置任务/代码(我们在根级Rails Rakefile以及其他地方有自定义代码).

Clearly, rake is running some important database setup tasks / code (we have custom code in the root level rails Rakefile and possibly other places).

如何在不运行rake spec的情况下运行rake测试数据库设置任务/代码?

How can I run the rake test database setup tasks / code without running rake spec?

除了能够在一部分文件上运行rspec之外,我还使用 specjour 进行传播我们跨多个核心的规格(尚未成功通过局域网分布它们),但是我看到了与直接运行rspec相同的行为:我需要在每个测试数据库(假设有两个核心)上运行rake spec作品:

In addition to being able to run rspec on a subset of files, I am using specjour to spread our specs across multiple cores (haven't had success with spreading them across the LAN yet), but I see the same behavior as for running rspec directly: I need to run rake spec on each test database (assuming two cores) before specjour works:

rake spec TEST_ENV_NUMBER=1
control-c (after tests start)
rake spec TEST_ENV_NUMBER=2
control-c (after tests start)
specjour

注意:我的config/database.yml具有用于测试的条目(对于并行测试gem来说很常见):

Note: my config/database.yml has this entry for test (as is common for the parallel testing gems):

test:
  adapter: postgresql
  encoding: unicode
  database: test<%=ENV['TEST_ENV_NUMBER']%>
  username: user
  password:

parallel_tests 似乎正确设置了其数据库,但是我们的许多规格都失败了.

parallel_tests seems to set up its databases correctly, but many of our specs fail.

我还应该提到,运行specjour prepare会导致Postgres记录无法找到数据库的错误,但是会创建数据库(没有表).在随后的运行中,不会记录任何错误,但是也不会创建表.我的整个问题很可能只是prepare中的一个错误,所以我在github上报告了这个问题.

I should also mention that running specjour prepare causes Postgres to log errors that it can't find the databases, but it creates them (without tables). On a subsequent run, no errors are logged, but also no tables are created. It is possible that my whole issue is simply a bug in prepare, so I reported it on github.

我认为我可以通过在.specjour/hooks.rb中设置Specjour::Configuration.prepare在每个specjour测试数据库上运行任意代码,因此,如果有任何我需要运行的rake任务或其他代码,则可以在其中运行./p>

I think that I can run arbitrary code on each specjour test database by setting Specjour::Configuration.prepare in .specjour/hooks.rb, so if there's any rake tasks or other code that I need to run, it may work there.

推荐答案

我在工作中设置CI系统时遇到了类似的问题,因此我逐步建立了一个系统来处理此问题.它可能不是最好的解决方案,但是它对我来说很有效,并且我一直在寻找更好的做事方法.

I had a similar problem setting up the CI system at work, so I gradually worked up a system to handle this. It may not be the best solution, but it works for me in my situation and I'm always on the lookout for better ways to do things.

我有一个需要设置的测试数据库,但还需要加载种子数据才能使我们的测试正常工作.

I have a test database that I needed setup, but also needed seeded data loaded for our tests to work.

对rake任务进行故障排除的基本原理是使用--trace选项运行rake,以了解幕后情况.当我这样做时,我发现运行rake spec可以做很多事情,我可以在自定义rake任务中复制(或修改为我认为合适的东西).

The basics of troubleshooting rake tasks is to run rake with the --trace option to see what is happening under the hood. When i did this, I found that running rake spec did a number of things that I could replicate (or modify as I saw fit) in a custom rake task.

这是我们所做的事的一个例子.

Here's an example of what we do.

desc "Setup test database - drops, loads schema, migrates and seeds the test db"
task :test_db_setup => [:pre_reqs] do
  Rails.env = ENV['RAILS_ENV'] = 'test'
  Rake::Task['db:drop'].invoke
  Rake::Task['db:create'].invoke
  result = capture_stdout { Rake::Task['db:schema:load'].invoke }
  File.open(File.join(ENV['CC_BUILD_ARTIFACTS'] || 'log', 'schema-load.log'), 'w') { |f| f.write(result) }
  Rake::Task['db:seed:load'].invoke
  ActiveRecord::Base.establish_connection
  Rake::Task['db:migrate'].invoke
end

这仅是一个示例,特定于我们的情况,因此您需要弄清楚需要执行哪些操作才能设置测试数据库,但是使用rake的--trace选项可以很容易地确定它

This is only an example, and specific to our situation, so you'll need to figure out what needs to be done to get your test db setup, but it is quite easy to determine using the --trace option of rake.

此外,如果您发现测试设置花费的时间太长(在我们的示例中如此),则还可以将数据库转储为.sql格式,然后将测试数据库直接将其通过管道传递到mysql中进行加载.这样,我们可以节省测试数据库设置的几分钟时间.我在这里没有显示出来,因为它使事情变得非常复杂-必须正确地生成它,而又不会过时.

Additionally, if you find the test setup is taking too long (as it does in our case), you can also dump the database into .sql format and have the test database pipe it directly into mysql to load. We save several minutes off the test db setup that way. I don't show that here because it complicates things substantially -- it needs to be generated properly without getting stale, etc.

HTH

这篇关于如何在不运行rake spec的情况下为Rails rspec测试准备测试数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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