RSpec controller testing - blank response.body

Toms Mikoss picture Toms Mikoss · Jun 30, 2009 · Viewed 26.7k times · Source

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.

Answer

mislav picture mislav · Jun 30, 2009

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