【问题标题】:How can i add a task that will run after rake test:db:prepare in Rails如何在 Rails 中添加将在 rake test:db:prepare 之后运行的任务
【发布时间】:2015-02-26 21:27:56
【问题描述】:

我正在使用 rails 4.2,我正在尝试使用命令 rake test 运行测试。我正在尝试使用 plv8 扩展,所以我从 psql 控制台手动创建它,然后当我运行测试时,它似乎已经删除了来自 postgres 的扩展。我查看了 rails 4.2 项目,我注意到他们带来了 test:db:prepare back。这就是每次删除 plv8 扩展的原因。如何添加一段将在 test:db:prepare 或 test:db:create 之后运行的代码?

我不喜欢修改 Rakefile 解决方案。

【问题讨论】:

    标签: ruby-on-rails ruby postgresql rake ruby-on-rails-4.2


    【解决方案1】:

    您可以内联命名多个任务:

    rake test:db:create test:db:prepare custom:task
    

    或者当您创建一个新的 rake 任务时,您可以使其依赖于任何其他任务:

    desc "the dependent task will run before this one"
    task my_task: :other_task do
       # stuff
    end
    

    你也可以任意调用其他任务:

    Rake::Task['db:test:prepare'].invoke
    

    更多信息在这里:http://jasonseifer.com/2010/04/06/rake-tutorial 和这里:How to run Rake tasks from within Rake tasks?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-08-04
      • 2016-02-22
      • 2016-06-03
      • 2019-04-13
      • 2013-05-12
      • 2013-08-05
      • 2010-10-09
      相关资源
      最近更新 更多