Relying on rake task to do some repetitive work is a really common way in most
rails apps. Testing those tasks seems to not be as common so.
Personally I think this is mainly due to just not being present in the default
tests stack, so as a developer I assume they are not worth testing because nobody
else is. But actually I think a task which is essential to your application
should be tested never the less, so to ease my journey towards better test
coverage for rake tasks in my own apps I decided to create a little helper to
setup just the basic environment and about as important, be there as a reminder
that I should test those tasks.
Personally I come to like minitest to test rails apps, so the
gist is based on
minitest, but can easily be adapted for any other framework. Looking through the
code you’ll find some conventions I personally like to be enforced in my rake
- namespace the task under the app name if it directly interacts with the app
- put the tasks in sub directories under lib/tasks so it does not become a
junk drawer of code
- put each task in its own file
- name the file after the task (common sense I guess but not always true)
so here we go, enjoy!