4

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!

Ultimation
  • 1,059
  • 9
  • 18
  • using environment variables for rake tasks is not the default way. you can add it to the rake calls directly like rake commissions:create_price_points[1,2,3] please go and read the docs. – phoet Oct 18 '13 at 19:00

1 Answers1

4

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'
Community
  • 1
  • 1
Peter Alfvin
  • 28,599
  • 8
  • 68
  • 106