I have a simple rake task with a puts
in it
namespace :atask
task something: :environment do
puts 'Foo'
end
end
My spec is as follows
describe 'atask:something' do
before { MyApp.Application.load_tasks }
context 'one' do
it do
Rake::Task['atask:something'].invoke
end
end
context 'two' do
it do
Rake::Task['atask:something'].invoke
end
end
context 'three' do
it do
Rake::Task['atask:something'].invoke
end
end
end
As you can see it has 3 contexts and 3 it blocks. So I should see 'Foo' in the console three times, once for each time the task is invoked. However, I only see 'Foo' once in the console even though it is invoked three times.
Whenever you are testing Rake tasks, you need to load the tasks from the Rails application itself. Note that in your tests you should change MyApplication to the name of your application. This line locates the task by it's name and returns a Rake::Task object. Then, we call invoke on it, which executes the task.
Rake is a software task management and build automation tool created by Jim Weirich. It allows the user to specify tasks and describe dependencies as well as to group tasks in a namespace. It is similar in to SCons and Make.
RSpec is a testing tool for Ruby, created for behavior-driven development (BDD). It is the most frequently used testing library for Ruby in production applications. Even though it has a very rich and powerful DSL (domain-specific language), at its core it is a simple tool which you can start using rather quickly.
It turns out that when a rake task has been invoke
d it won't be ran again. You have to either reenable
the task after invocation or use execute
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With