使用 rspec 中传递的参数测试 rake 任务 [英] Testing a rake task with passed parameters in rspec

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

问题描述

在 rspec 中,我想用传入的一些参数测试一个 rake 任务,因此在命令行中您可以运行:

In rspec, i want to test a rake task with some parameters passed in, so in the command line you would run this:

rake commissions:create_price_points options=1,2,3

在 rake 任务中我使用 ENV['options'].

and in the rake task i use ENV['options'].

在我的 rspec 中,我有

In my rspec I have

rake = get_rake_environment(RAKE_FILE)
rake["commissions:create_price_points"].invoke(options=1,2,3)

这可以很好地运行 rake,但是这和我使用 invoke 和 execute 所做的其他尝试,不会将选项传递给它.只是想知道是否有人对如何做到这一点有任何见解.谢谢!

This runs the rake fine but this, and other attempts that I've made with invoke and execute, do not pass the options into it. Just wondering if anyone has any insight on how to do this. Thanks!

推荐答案

传递给 invoke 方法的参数与在 rake 命令行中传递的参数不同.在 RSpec(即 Ruby)中,表达式 options=1,2,3 将数组 [1,2,3] 分配给局部变量 options 并将该数组传递给 invoke.当由 invoke 方法接收时,该数组被视为 rake 任务的正式参数.(有关此方法的更多信息,请参阅https://stackoverflow.com/a/825832/1008891.)

The arguments passed to the invoke method are not the same as those passed in the rake command line. In RSpec, which is Ruby, the expression options=1,2,3 assigns the array [1,2,3] to the local variable options and passes that array to invoke. When received by the invoke method, the array is treated as a formal argument to the rake task. (See https://stackoverflow.com/a/825832/1008891 for more information on this approach.)

由于您的 rake 任务需要设置环境变量 options,因此您需要在调用它之前进行设置,如下所示:

Since your rake task is expecting the environment variable options to be set, you need to set that prior to invoking it, as in:

ENV['options'] = '1,2,3'

这篇关于使用 rspec 中传递的参数测试 rake 任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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