Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting blank page when running RSpec tests for get 'new'

Tags:

rspec

capybara

I am getting an empty page as response when running the following RSpec test:

require 'spec_helper'

describe FriendshipsController do
  include Devise::TestHelpers
  render_views

  before(:each) do
    @user = User.create!(:email => "[email protected]", :password => "mustermann", :password_confirmation => "mustermann")
    @friend = User.create!(:email => "[email protected]", :password => "password", :password_confirmation => "password")    
    sign_in @user
  end  

  describe "GET 'new'" do

    it "should be successful" do
      get 'new', :user_id => @user.id
      response.should be_success
    end

    it "should show all registered users on Friendslend, except the logged in user" do
      get 'new', :user_id => @user.id

      page.should have_select("Add new friend")
      page.should have_content("div.users")
      page.should have_selector("div.users li", :count => 1)
    end

    it "should not contain the logged in user" do
      get 'new', :user_id => @user.id
      response.should_not have_content(@user.email)
    end
  end
end

I only get a blank page when running the RSpec test. With blank page I mean there is no other HTML content other than the DOCTYPE declaration.

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">

Interestingly, RSpec tests for post 'create' work fine. Any hints?

I am using Rails 3.2 with spec-rails, cucumber and capybara (instead of webrat).

like image 479
Patrick Frey Avatar asked Feb 23 '12 19:02

Patrick Frey


2 Answers

I was able to solve this by adding the following in my spec_helper.rb file:

RSpec.configure do |config|
  config.render_views
end

Optionally you can call render_views in each controller spec individually.

https://github.com/rspec/rspec-rails/blob/master/features/controller_specs/render_views.feature

like image 63
Jimmy Baker Avatar answered Dec 07 '22 00:12

Jimmy Baker


The problem is that you are mixing types of tests. Capybara, which provides the page object is used in request specs by calling visit path.

In order to fix your problem, you need to be looking at the response object instead of the page object.

If you want to test content with capybara, the way that you would build that test would look something like this:

visit new_user_session_path
fill_in "Email", :with => @user.email
fill_in "Password", :with => @user.password
click_button "Sign in"
visit new_friendships_path(:user_id => @user.id)
page.should have_content("Add new friend")

That code should be placed in a request spec instead of a controller spec, by convention.

like image 42
Daniel Evans Avatar answered Dec 07 '22 00:12

Daniel Evans