Rspec: Expecting a Rake task to be called

Updated . Posted . Visible to the public.

This seems to be obvious, but you can expect Rake tasks to be called in RSpec.

it 'deletes all Users' do
   FactroyBot.create(:user)
   expect(Rake::Task['notify:critical_operation']).to receive(:invoke)
   
   expect { described_class.clean }.to change(User, :count).from(1).to(0) 
end

Note: Try to avoid logic in rake tasks and prefer to just call classes in them.

Example:

desc 'Some task'
task :some_task do
  SomeClass.new.run
end
Last edit
Felix Eschey
License
Source code in this card is licensed under the MIT License.
Posted by Emanuel to makandra dev (2020-05-28 11:58)