Ultimation
Ultimation

Reputation: 1069

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:

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!

Upvotes: 4

Views: 2158

Answers (1)

Peter Alfvin
Peter Alfvin

Reputation: 29399

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'

Upvotes: 4

Related Questions