Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rspec testing rake tasks

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.

like image 612
Cjmarkham Avatar asked Oct 01 '15 13:10

Cjmarkham


People also ask

How do I test rake tasks?

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.

What is a Rake 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.

What is RSpec testing?

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.


1 Answers

It turns out that when a rake task has been invoked it won't be ran again. You have to either reenable the task after invocation or use execute

like image 159
Cjmarkham Avatar answered Sep 20 '22 09:09

Cjmarkham