Karol Selak
Karol Selak

Reputation: 4804

`Rake::Task['db:seed'].invoke` doesn't work - "Don't know how to build task 'db:seed'"

I have a problem with testing my rake task. The task looks like this:

namespace :db do
  desc 'Load the seed data from db/seeds.rb'
  task :seed => :environment do
    puts 'seed'
  end
end

And my test like this:

require 'rake'
require 'spec_helper'

RSpec.describe Rake::Task do
  describe "db:seed" do
    it "runs a task" do
      Rake::Task['db:seed'].invoke
    end
  end
end

Everything looks ok, but I'm still getting that error:

 Failure/Error: Rake::Task['db:seed'].invoke
   RuntimeError:
     Don't know how to build task 'db:seed' (See the list of available 
     tasks with `rake --tasks`)

What's the most strange, the same task works when calling via console:

> rake db:seed
seed

Some ideas?

Upvotes: 1

Views: 1692

Answers (2)

trushkevich
trushkevich

Reputation: 2677

To be able to invoke a task via

Rake::Task['...'].invoke

you should first load tasks via

Rails.application.load_tasks

Upvotes: 8

Karol Selak
Karol Selak

Reputation: 4804

Okay, I found a bypass:

RSpec.describe Rake::Task do
  describe "db:seed" do
    it "runs a task" do
      system('rake db:seed')
    end
  end
end

Upvotes: 0

Related Questions