The reason this doesn't work is that controllers are Rack middleware and expect to be initialized with a request. Its also just not how you should be coding or testing your code in the first place. Bin it.
Controller specs use the get
, post
, etc methods that actually create an instance of the controller with a mocked request.
get :foo
would then call the foo
method directly on the controller instance. You do not initialize controllers directly. If you feel tempted to do this its a very good sign you're doing something very wrong.
Unit testing controllers with controller specs is an extremely flawed approach that's discouraged by both the RSpec and Rails teams - you're mocking out huge parts of the application like the middleware and routes and it lets so many bugs through your tests.
The modern way to test a Rails application is to just test the actions of your controller by sending HTTP requests. This means that you should ONLY test the methods that actually correspond to a route and test your application through the response it provides (the headers and body) and eventually the side effects. In RSpec-Rails this is provided through request, feature and system specs.
Everything else in your controller like this method should be private - and you don't test privates.