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

查看:27
本文介绍了如何在不运行 rake 规范的情况下为 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 来传播我们在多个内核上的规范(还没有成功地将它们传播到 LAN),但我看到了与直接运行 rspec 相同的行为:我需要在每个测试数据库上运行 rake spec(假设有两个内核)在 specjour 工作之前:

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 任务或其他代码,我需要运行,它可能在那里工作.

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 规范的情况下为 Rails rspec 测试准备测试数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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