简体   繁体   English

在rspec中使用传递的参数测试rake任务

[英]Testing a rake task with passed parameters in rspec

In rspec, i want to test a rake task with some parameters passed in, so in the command line you would run this: 在rspec中,我想测试一个rake任务,传入一些参数,所以在命令行中你会运行这个:

rake commissions:create_price_points options=1,2,3

and in the rake task i use ENV['options']. 在rake任务中我使用ENV ['options']。

In my rspec I have 在我的rspec我有

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

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. 这运行rake很好,但是这和我用invoke和execute做的其他尝试都没有将选项传递给它。 Just wondering if anyone has any insight on how to do this. 只是想知道是否有人对如何做到这一点有任何见解。 Thanks! 谢谢!

The arguments passed to the invoke method are not the same as those passed in the rake command line. 传递给invoke方法的参数与rake命令行中传递的参数不同。 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 . 在RSpec中,它是Ruby,表达式options=1,2,3将数组[1,2,3]分配给局部变量options并传递该数组以进行invoke When received by the invoke method, the array is treated as a formal argument to the rake task. 当被invoke方法接收时,该数组被视为rake任务的形式参数。 (See https://stackoverflow.com/a/825832/1008891 for more information on this approach.) (有关此方法的更多信息,请参阅https://stackoverflow.com/a/825832/1008891 。)

Since your rake task is expecting the environment variable options to be set, you need to set that prior to invoking it, as in: 由于您的rake任务期望设置环境变量options ,因此您需要在调用它之前设置它,如:

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

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

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