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