Mert Bulan

Product-Minded Developer

How to test the same Rake task multiple times

In some cases, you may want to test the same Rake task multiple times with different arguments. This post shows you how to do that.

A couple of weeks ago, I was working on a task that required me to create a rake task. The rake task was supposed to be run in two different ways, one without any arguments and the other with an argument. I wrote the rake task and it was time to write the tests. I wrote the test for the rake task without any arguments and it passed. I then wrote the test for the rake task with an argument and it failed. It failed in a way that the task was not being run at all. I was confused.

After searching on the internet, I found out that the rake task was not being run because the task was already executed in the first test and it wasn’t possible to execute it again before reenabling the task.

after do
  Rake::Task['my_rake_task'].reenable
end

This is how I was able to test the same rake task multiple times. I hope this helps you too.

Subscribe to my blog

No spam, no sharing to third party. Only you and me.

Member discussion