Last Updated: February 25, 2016
·
1.608K
· fourcolors

Thoughts on testing Rake tasks in Rails

My philosophy

Rake tasks are traditionally a pain to test. Josh from Thoughtbot has a wonderful article walking you through the process here: http://robots.thoughtbot.com/post/11957424161/test-rake-tasks-like-a-boss

After reading the article and following his example to test my own rake tasks I came to the conclusion that when testing rake tasks they should work like integration tests. Put all of your functionality in a model or class then unit test that class. Within your actual rake task you should just check to see if the methods are being called on the given classes with something like "describedclass.shouldreceive(:foo)" This will make your rake tasks both easier to maintain and test.

I'll update this with examples soon.