← Back to Upcase

Testing Rake tasks (like a BOSS) and Simplecov


(Geoff Harcourt) #1

After reading @joshclayton’s awesome post about testing Rake tasks (http://robots.thoughtbot.com/test-rake-tasks-like-a-boss), I have been using some of the shared context code he wrote to test my own Rake tasks. Usually all my test is doing is verifying that some class I built, like WeatherConditionsFetchTask, had its #go method called, and the nitty-gritty of the functionality it tested somewhere else.

I have noticed an unpleasant side-effect of this shared context, however, and I was wondering if Thoughtboters had found a solution: when I run all my specs using Simplecov (or the CodeClimate test coverage tool, which seems to be using Simplecov under the hood), only one of my Rake tests will show up as being covered in the test coverage report, even though when I run RSpec with verbose (-fd flag) spec output, I can clearly see that all the Rake tests are being run. I suspect that somewhere in the subject-setting magic of the shared context the references to the prior tests are getting blown away.

So if any of you use Simplecov, have you had a similar issue, also, since this shared context was written in late 2011, does Thoughtbot use an updated version of this code that is more compatible with test coverage tools?


(Josh Clayton) #2

@geoffharcourt I haven’t found a good way to get SimpleCov and RSpec’s shared contexts (or shared examples) to work well when calculating test coverage. The only thing I think might work would be extracting this out into a different object which rolls up most of this and makes it available without any shared contexts - that is, you could create an object instantiated with the rake task as a string (e.g. 'cron:hourly') and exposes pieces of information like prerequisites.


(Geoff Harcourt) #3

@joshclayton, thanks for the feedback. I suspected that there was something about that approach that wouldn’t work.