Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RSpec controller testing - blank response.body

I am stuck with a problem when testing my controllers with RSpec - the response.body call always returns an empty string. In browser everything renders correctly, and cucumber feature tests seem to get it right, but RSpec fails each and every time.

Other expectations on the response object, such as response.should render_template('index') pass without any problems.

Have any of you encountered this problem before? Perhaps the response html can be obtained in some other way?

As for versions, Rails 2.1.0, RSpec 1.2.7.

like image 588
Toms Mikoss Avatar asked Jun 30 '09 11:06

Toms Mikoss


2 Answers

By default, rspec-rails hacks into Rails to prevent it from actually rendering view templates. You should only test the behavior of your actions & filters your controller tests, not the outcome of template rendering — that's what view specs are for.

However, if you wish to make your controller specs render templates as the app normally would, use the render_views directive:

describe YourController do   render_views   ... end 
like image 52
mislav Avatar answered Oct 09 '22 21:10

mislav


RSpec 2+: If you want to check end to end - url to response body - use a request spec instead of a controller spec.

like image 25
Thomas Avatar answered Oct 09 '22 21:10

Thomas