在 Rails 环境中使用 Rspec 测试 Rake 任务 [英] Testing Rake task with Rspec with Rails environment

查看:55
本文介绍了在 Rails 环境中使用 Rspec 测试 Rake 任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试测试一个 rake 任务,它在其中使用了一个活动记录.

I'm trying to test a rake task and it uses an active record in it.

require 'spec_helper'
require 'rake'

load File.join(Rails.root, 'lib', 'tasks', 'survey.rake')

describe "survey rake tasks" do
  describe "survey:send_report" do
    it "should send a report" do
      Rake::Task['survey:send_report'].invoke
    end
  end
end

当我运行这个规范 rspec spec/lib/survey_spec.rb 时,我得到这个错误

When I run this spec rspec spec/lib/survey_spec.rb, I get this error "

RuntimeError:
   Don't know how to build task 'environment'

如何通过示例规范在内部加载 :enviroment 任务?

How do I load the :enviroment task inside by example spec?

推荐答案

我认为你应该先加载任务:

I think you should first load the tasks:

require 'rake'
MyRailsApp::Application.load_tasks

然后调用您的任务:

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

这篇关于在 Rails 环境中使用 Rspec 测试 Rake 任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆