gpt4 book ai didi

ruby-on-rails - 在 `rake test` 之后执行 rake 任务

转载 作者:数据小太阳 更新时间:2023-10-29 07:52:11 27 4
gpt4 key购买 nike

我正在尝试为 Rails 4.0.2 (Ruby 2.2.3) 项目创建一个 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 任务完成运行之前被调用。在前面的任务完成后,是否有调用清理 rake 任务的方法?

这是使用 trace 运行任务的输出:

$ 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.

如您所见,任务是按顺序调用的,但清理任务是在测试完成之前执行的。有解决这个问题的想法吗?

最佳答案

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

关于ruby-on-rails - 在 `rake test` 之后执行 rake 任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33811821/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com