views:

149

answers:

1

Normally to pass parameters via in RSpec we do:

params[:my_key] = my_value
get :my_method

Where my_method deals with what it received from params. But in my controller I have a method, which takes args directly i.e.:

def my_method(*args)
  ...
end

How do I call the method with those args from within the test? I've tried get :my_method(args) but Ruby interpreter complains about syntax error.

+1  A: 

As far as I can see, you can't do that. You can extract my_method into a helper, or into a library, and then test it with Rspec, if you need to test my_method directly. Or you can structure your controller tests so that the code in my_method is exercised as part of a controller action that can be called by one of the standard HTTP verbs (get, post, put, delete).

Ultimately it has to be possible to reach any code that your controller uses by setting up tests with get, post, etc, because otherwise Rails couldn't do it and your app wouldn't work. This may make testing inconvenient, but it might also be telling you that my_method would be better off living outside the controller.

zetetic