简体   繁体   English

编写多个 Rake 任务

[英]Writing multiple Rake tasks

I've written two Rspec tests each which invoke the same Rake task.我编写了两个 Rspec 测试,每个测试调用相同的 Rake 任务。 The second task never gets run, as invoke only triggers once so I need to reenable.第二个任务永远不会运行,因为调用只触发一次,所以我需要重新启用。 My issue is that I can't get the rake task to run, here is the command I'm using:我的问题是我无法运行 rake 任务,这是我正在使用的命令:

Rake::Task["product:delete"].reenable(product.id)

I get a run time error for this command:我收到此命令的运行时错误:

Don't know how to build task 'product:delete[1]' Did you mean?不知道如何构建任务 'product:delete[1]' 是吗? product:delete产品:删除

Anybody know how I should write this?任何人都知道我应该怎么写这个? I'm confused because in isolation I get it to pass by running:我很困惑,因为在孤立的情况下,我通过运行让它通过:

Rake.application.invoke_task("product:delete[#{product.id}]"

You shouldn't need to pass an argument to reenable (it doesn't take one)你不应该需要传递一个参数来reenable (它不需要一个)

You should however be passing your argument to invoke (not invoke_task) rather than specifying it as part of the task name.但是,您应该将参数传递给invoke (而不是 invoke_task),而不是将其指定为任务名称的一部分。

Eg例如

Rake::Task['product:delete'].reenable
Rake::Task['product:delete'].invoke(product.id)

You could streamline it a little more perhaps by saving the task in a variable:您可以通过将任务保存在变量中来简化它:

t = Rake::Task['product:delete']
t.renable
t.invoke(product.id)

PS It looks a lot like that error "Don't know how to build task 'product:delete[1]' Did you mean? product:delete" has actually come from you trying to invoke the task with the argument in the task name rather than from the reenable. PS 它看起来很像那个错误“不知道如何构建任务‘product:delete[1]’你是说吗?product:delete”实际上来自你试图用任务名称中的参数调用任务而不是从重新启用。 Possibly as a result of trying a lot of different things.可能是因为尝试了很多不同的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM