简体   繁体   English

“ Rake :: Task ['db:seed']。invoke”不起作用-“不知道如何构建任务'db:seed'”

[英]`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? 有什么想法吗?

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

To be able to invoke a task via 能够通过调用任务

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

you should first load tasks via 您应该首先通过加载任务

Rails.application.load_tasks

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM