It’s tricky to test rake tasks for following reasons.
- It’s annoying to see the task’s outputs when running tests if you set it up so.
- rake does not allow you to run the same task for multiple test examples as it is.
First you want to create a tag so that you can apply the same setup for all the rake task tests. Here I named it ‘rake_test’.
Here’s a sample of such cases with Ruby on Rails.
It suppresses unnecessary outputs from the tasks when you run RSpec. File::NULL usually goes to /dev/null. We keep the original $stdout and $stderr as instance variables here, so that we can put them back later. If, however, you are sure original $stdout is always STDOUT and $stderr is STDERR, the code above can be shorter.
“define_task” is necessary to load Rails environment which is “test” in this case. And each test should be like so:
“invoke_task” runs your rake task as its name claims. “reenable” allows the task to be executed again if the task is invoked again.
Another note is that these tests run basically in the same process as other types of tests whereas rake tasks run in a separated process in daily operatioins. So if you overwrite something in rake task tests, you have to put the original back at the end.