Skip to content

Instantly share code, notes, and snippets.

@waterlink
Created March 18, 2014 15:15
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 waterlink/9622045 to your computer and use it in GitHub Desktop.
Save waterlink/9622045 to your computer and use it in GitHub Desktop.
RSpec setup for testing rake tasks
# spec/support/rake.rb
require 'rake'
module Rake::TestHelpers
def run_task(name=subject)
Rake::Task[name].reenable
Rake.application.invoke_task name
end
end
RSpec.configure do |config|
config.include Rake::TestHelpers, type: :task
config.before(:each, type: :task) do
Rake::Task.define_task :environment
Rake.application.rake_require example.metadata[:task]
end
end
# spec/tasks/admin_spec.rb
require 'spec_helper'
describe task: 'tasks/admin', type: :task do
before :each { Admin.destroy_all }
describe 'create' do
subject { 'admin:create' }
before { run_task }
it 'creates one admin' do
expect(Admin.count).to be 1
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment