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!

1
  • 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. Commented Oct 18, 2013 at 19:00

1 Answer 1

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'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.