On testing rake files

I started to be concerned about how to test Rake files just recently.

There was an issue for one of the gems I wrote. The gem is mostly a bunch of Rake tasks.

Here’s an example of such a task:

  task :create do
    rakefile = File.expand_path('Rakefile', dummy_path)
    command = "rake -f '%s' db:create" % rakefile
    sh(command) unless ENV["DISABLE_CREATE"]

Now following the articles from Thoughtbot and Tyler, and learning how I could write my tests, I tried to put together hopefully the best of both approaches into a gem I could reuse.

I wrote RSpec Rake, which should provide two important things that could help write rake tests easily:

Now using rspec-rake, here’s an example test for the task from above:

# spec/tasks/dummy_create_spec.rb

require 'spec_helper'

describe 'dummy:create' do
  it 'calls rake with db:create task' do
    rakefile = File.expand_path('../../dummy/Rakefile', __FILE__)
    command = "rake -f '%s' db:create" % rakefile
    # An equivalent of mocking `sh` method

Hopefully this will motivate others to write more Rake task tests too.
For more documentation check the Github repository.


Now read this

The last 7 months of our venture

Roughly a year ago I joined a venture which would become one of the most memorable and teaching journey in my career, that I had a chance to be a part of so far. This is my point of view on the last days of probably the most dynamic and... Continue →