2015-11-19 96 views
3

我正在尝试为Rails 4.0.2(Ruby 2.2.3)项目创建Rake任务,该项目与种子一起创建测试数据库,然后运行测试套件,并且最后通过删除测试数据库进行清理。下面是下面一个简单的例子:'rake test'后执行rake任务

task better_test: :environment do 
    Rake::Task["test:setup"].execute 
    Rake::Task["test"].execute 
    Rake::Task["test:cleanup"].execute 
end 

上面的任务的确调用test:setup rake任务(创建/种子测试数据库),然后调用test rake任务,最后调用test:cleanup rake任务。然而,问题是最后的test:cleanuptest rake任务完成运行之前被调用。无论如何要在完成前面的任务后调用清理耙子任务吗?

下面是从跟踪运行任务的输出:

$ RAILS_ENV=test be rake better_test --trace 
/usr/local/rvm/gems/ruby-2.2.3/gems/activesupport-4.0.2/lib/active_support/values/time_zone.rb:282: warning: circular argument reference - now 
/usr/local/rvm/gems/ruby-2.2.3/gems/honeybadger-1.16.3/lib/honeybadger/rack/user_feedback.rb:51: warning: circular argument reference - action 
** Invoke better_test (first_time) 
** Invoke environment (first_time) 
** Execute environment 
** Execute better_test 
** Execute test:setup 
Creating test database. 
** Execute db:test:setup 
** Execute db:test:create 
** Execute db:create 
** Execute db:test:load 
** Execute db:schema:load 
** Execute db:test_project:setup 
** Execute db:test_project:create 
** Invoke db:create (first_time) 
** Invoke environment 
** Execute db:create 
** Execute db:test_project:migrate:down 
** Execute db:test_project:migrate:up 
** Execute db:test_project:seed 
** Execute test 
** Invoke test:run (first_time) 
** Invoke test:units (first_time) 
** Invoke test:prepare (first_time) 
** Invoke db:test:prepare (first_time) 
** Invoke db:abort_if_pending_migrations (first_time) 
** Invoke environment 
** Invoke db:migrate:load (first_time) 
** Invoke environment 
** Execute db:migrate:load 
** Execute db:abort_if_pending_migrations 
** Execute db:test:prepare 
** Execute db:drop 
** Execute db:create 
** Execute db:load 
** Invoke db:schema:load (first_time) 
** Invoke environment 
** Execute db:schema:load 
** Execute test:prepare 
** Execute test:units 
** Invoke test:functionals (first_time) 
** Invoke test:prepare 
** Execute test:functionals 
** Invoke test:integration (first_time) 
** Invoke test:prepare 
** Execute test:integration 
** Execute test:run 
** Invoke test:decorators (first_time) 
** Invoke test:prepare 
** Execute test:decorators 
** Execute test:cleanup 
** Execute db:test:drop 
** Execute db:drop 
** Execute db:test_project:drop 
** Invoke db:drop (first_time) 
** Invoke environment 
** Execute db:drop 
Run options: --seed 19360 

# Running tests: 

EE.... 

Finished tests in 0.037493s, 160.0278 tests/s, 106.6852 assertions/s. 

正如你可以看到任务调用,以便但测试完成之前执行清理任务。任何想法来解决这个问题?

回答

2

事实证明,这与耙子有关,而与Minitest执行测试的方式有关。我在问题中提到,似乎rake任务正在以正确的顺序被调用,但测试由于某种原因而稍后执行。原因是Minitest使用红宝石内核方法at_exit来执行测试。在调用rake test时读取测试文件,但所有测试都在ruby程序结束时执行,即使在我后续的rake任务之后也是如此。这里有一篇博客文章更详细地解释了这个问题:http://blog.arkency.com/2013/06/are-we-abusing-at-exit/

+0

你有没有解决这个问题? – neeraj

2

尝试使用耙标准语法调用多个任务:

task better_test: ["test:setup", "test", "test:cleanup"] 

也许这将解决你的问题。

另外,似乎Rake实际上在执行它们之前建立了它应该运行的所有任务的列表,并且只运行一次。例如:

task :a 
task :b 
task :c 

task better_test: ["a", "b", "c", "b"] 

结果:

$ rake better_test -t 
** Invoke better_test (first_time) 
** Invoke a (first_time) 
** Execute a 
** Invoke b (first_time) 
** Execute b 
** Invoke c (first_time) 
** Execute c 
** Execute better_test 

正如你看到的,任务b只运行一次(第一个)。我相信你的test:setuptest莫名其妙取决于test:cleanup任务。所以它比预期的要早。

+0

德米特里,这是一个好主意,但结果相同。我真的觉得这与耙子更不相干,更多的是与minitest如何执行测试有关。 –

+0

我在回答中增加了一些其他的想法,可能是你的'test:setup'或'test'已经调用了清理(不知道为什么,但也许你或者一些gem增加了依赖来运行'cleanup')它根本不叫第二次。 –

0
Rake::Task["test"].enhance do 
    Rake::Task["test:cleanup"].invoke 
end 
+0

感谢Marcin。我也尝试过这个路由,但是执行清理任务后测试仍然运行。它们是按顺序调用的,但第二个任务不会等待第一个任务完成。 –