测试在 rake 任务中定义的方法 [英] Testing a method defined in a rake task

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

问题描述

我想测试在 rake 任务中定义的方法.

I want to test a method defined in a rake task.

rake 文件

#lib/tasks/simple_task.rake
namespace :xyz do
    task :simple_task => :environment do
        begin
            if task_needs_to_run?
                puts "Lets run this..."
                #some code which I don't wish to test
                ...
            end
        end
    end
    def task_needs_to_run?
        # code that needs testing
        return 2 > 1
    end

end

现在,我想在测试文件中测试这个方法,task_needs_to_run?我该怎么做?

Now, I want to test this method, task_needs_to_run? in a test file How do I do this ?

附加说明:我也希望在 rake 任务中测试另一个私有方法......但我可以稍后再担心.

Additional note: I would ideally want test another private method in the rake task as well... But I can worry about that later.

推荐答案

你可以这样做:

require 'rake'
load 'simple_task.rake'
task_needs_to_run?
=> true

我自己试过了……在 Rake 命名空间中定义一个方法与在顶级定义它是一样的.

I tried this myself... defining a method inside a Rake namespace is the same as defining it at the top level.

load 加载 Rakefile 不会运行任何任务……它只是定义了它们.所以在测试脚本中load你的Rakefile并没有什么坏处,这样你就可以测试相关的方法.

loading a Rakefile doesn't run any of the tasks... it just defines them. So there is no harm in loading your Rakefile inside a test script, so you can test associated methods.

这篇关于测试在 rake 任务中定义的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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