49
loading...
This website collects cookies to deliver better user experience
Rake::DefaultLoader.new.load
.lib/tasks/
and organize them by namespace. So this task lives in lib/tasks/authors.rake
.namespace :authors do
desc 'Migrate Author Names'
task :migrate_names do
Authors.find_each do |author|
author.update!(full_name: "#{author.first_name} #{author.last_name")
end
end
end
rake authors:migrate_names
. Add this line to your Rakefile.Dir.glob('lib/tasks/*.rake').each { |r| load r }
rake authors:create
and will load a subject
named task
that returns the rake task.Rake::DefaultLoader
. Since we're not calling rake
directly in the tests, we need this line to tell the tests where our tasks live.spec/tasks
so that RSpec knows these are task
tests and need the TaskFormat
module loaded.
require 'rake'
module TaskFormat
extend ActiveSupport::Concern
included do
let(:task_name) { self.class.top_level_description.sub(/\Arake /, '') }
let(:tasks) { Rake::Task }
# Make the Rake task available as `task` in your examples:
subject(:task) { tasks[task_name] }
end
end
RSpec.configure do |config|
config.before(:suite) do
Dir.glob('lib/tasks/*.rake').each { |r| Rake::DefaultLoader.new.load r }
end
# Tag Rake specs with `:task` metadata or put them in the spec/tasks dir
config.define_derived_metadata(file_path: %r{/spec/tasks/}) do |metadata|
metadata[:type] = :task
end
config.include TaskFormat, type: :task
end
require_relative '../../support/tasks'
describe 'rake authors:migrate_names', type: :task do
let(:author_cls_double) { class_double(Author).as_stubbed_const(transfer_nested_constants: true) }
let(:author_double) { instance_double(Author, first_name: 'Cassidy', last_name: 'Scheffer') }
let(:expected_name) { 'Cassidy Scheffer'
before do
allow(author_cls_double).to receive(:find_each).and_yield(author_double)
allow(author_double).to receive(:update!).with({ full_name: expected_name })
task.execute
end
it 'updates the correct fields' do
expect(author_double).to have_received(:update!).with({ full_name: expected_name })
end
end