Skip to content

Instantly share code, notes, and snippets.

@stevenyap
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stevenyap/6afd0e193b53cc6ddbc4 to your computer and use it in GitHub Desktop.
Save stevenyap/6afd0e193b53cc6ddbc4 to your computer and use it in GitHub Desktop.
Rake task testing

Given a rake file with a task:

# lib/tasks/test_run.rake

desc 'Sample description of a rake task'
namespace :test do
  task run: :environment do
    # do something here that adds to database
  end
end

Then the rake test file will be:

# spec/tasks/test_run_rake_spec.rb

require 'rails_helper'

RSpec.describe 'test:run' do
  it 'runs the rake task' do
    MyApp::Application.load_tasks # load all rake tasks, get app name from config/application.rb
    Rake::Task.define_task(:environment) # load the environment for your rake tasks
    expect { Rake::Task['test:run'].invoke }.to change(SomeModel, :count).by(1)
  end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment