Search code examples
ruby-on-railsrspecrake

Writing multiple Rake tasks


I've written two Rspec tests each which invoke the same Rake task. 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::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

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}]"

Solution

  • You shouldn't need to pass an argument to reenable (it doesn't take one)

    You should however be passing your argument to invoke (not invoke_task) rather than specifying it as part of the task name.

    E.g.

    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)
    

    P.S. 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. Possibly as a result of trying a lot of different things.