views:

367

answers:

2

Howdy,

I'm having big trouble testing with rSpec's controller API. Right now I'm using a middleware authentication solution (Warden), and when I run the specs, the proxy added by the middleware is not there, and all the authentication tests are throwing NilPointerExceptions all over the place.

It seems rSpec is not adding the middleware to the final app on purpose, and I would like to know if there is a way to monkey patch rSpec in order to make that go.

I already tested the whole thing with cucumber, however this is a refactoring of an old authentication version and there is several Controller tests that depend on authentication logic in order to work.

Thanks in advance.

+1  A: 

It seems rSpec Controller API for Rails 2.x is not really using Rack on the controller call, so this is not possible to do. However there is a (non proven) solution pointed out by David Chelimsky. I'll put the answer he gave me on the rSpec-user mail list.

Controller specs don't go through rack in rails 2, so not really.

There are integration specs, which wrap the rails integration test API so you can do anything you can do in a rails integration test. Just move the files over to spec/integration, convert the requests from "get :index" to "get 'path/to/index'"and you should be pretty much good to go.

Let us know how that works out.

Cheers, David

I ended up mocking the ApplicationController on the authentication logic methods to do dummy stuff in order to make my specs run, I did this before having the suggestion pointed out by David, so I wouldn't know if that work. Either way I hope it help any other developer on the same situation.

Roman Gonzalez
A: 

This post on a related problem might shed some light and eventually lead to a solution for specifying middleware components in an rSpec configuration (I hope!)

https://rspec.lighthouseapp.com/projects/5645/tickets/963-request-is-nil#ticket-963-14

It doesn't really help with your specific Rails2 issue, but this query came up top for 'rspec middleware' so hopefully will help others who encounter the issue as they try the Rails3 beta and beyond.

keeran