Testing a rake task with passed parameters in rspec

怎甘沉沦 提交于 2019-12-10 17:19:24

问题


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

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

In my rspec I have

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. Just wondering if anyone has any insight on how to do this. Thanks!


回答1:


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.)

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'


来源:https://stackoverflow.com/questions/19453363/testing-a-rake-task-with-passed-parameters-in-rspec

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!