Helper for testing rake tasks in rails using minitest.
require 'test_helper' | |
# testing rake task app:awesome_report | |
# defined in file lib/tasks/app/awesome_report.rake | |
describe 'App::AwesomeReportTaskTest' do | |
it 'generates the awesomeness report' do | |
subject.invoke | |
assert File.exists?('awesomeness_report.csv') | |
end | |
end |
# require this in test_helper | |
class RakeTaskTestCase < ActiveSupport::TestCase | |
before do | |
require 'rake' | |
Rake::Task.define_task :environment | |
load rake_task_load_path | |
end | |
private | |
def subject | |
Rake::Task[task_name] | |
end | |
def rake_task_load_path | |
"#{_tasks_base_path}/#{_relative_task_file_path}" | |
end | |
def task_name | |
_relative_task_file_path.sub('/', ':').sub(/\.rake$/, '') | |
end | |
def _relative_task_file_path | |
"#{self.class.name.sub(/TaskTest$/, '').underscore}.rake" | |
end | |
def _tasks_base_path | |
File.expand_path("#{Rails.root}/lib/tasks") | |
end | |
end | |
class MiniTest::Spec | |
register_spec_type(/.*TaskTest/, RakeTaskTestCase) | |
end |
This comment has been minimized.
This comment has been minimized.
@al Thanks for the tip! |
This comment has been minimized.
This comment has been minimized.
Rails 5
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This comment has been minimized.
Thanks. You might want to run a
Rake::Task.clear
in anafter
block too, otherwise unpleasant stuff can happen to your day.