Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capybara + RSpec only sees blank pages in controller specs. Why?

I'm trying to write a controller spec for a simple controller. However, Capybara isn't seeing any page content. However, looking at the site's pages in my browser works just fine. What am I doing wrong?

T. Hanks!

My controller spec

My spec_helper.rb

My Gemfile

like image 471
Evan Walsh Avatar asked May 09 '11 04:05

Evan Walsh


1 Answers

You need to explicitly tell your controller spec that you want it to render views in order for this to work. Update your spec to look like this:

require 'spec_helper'

describe PostsController do
  render_views # Render this controller's views during spec execution.

  before do
    @post = Fabricate :post
  end

  # ...
end

This is described in rspec's readme. For a more detailed view, see rspec-rails' cucumber feature for 'render_views'.

Just one word of caution with this. There are reasons why this isn't default behaviour:

  • Arguably, you're mixing two concerns by testing the views at the same time as the controllers. Ryan Bigg (see comments) suggests your tests might be better thought of as integration tests, which usually live in spec/integration rather than spec/controller.
  • Rendering the views may slow down the execution of your tests considerably.

... Not saying you shouldn't do this, just saying you should be clear why you are.

Hope that helps.

like image 58
Paul Russell Avatar answered Sep 20 '22 08:09

Paul Russell