Getting a blank page when running RSpec tests to get a "new"

I get a blank page as an answer when I run the following RSpec test:

require 'spec_helper' describe FriendshipsController do include Devise::TestHelpers render_views before(:each) do @user = User.create!(:email => " max@mustermann.com ", :password => "mustermann", :password_confirmation => "mustermann") @friend = User.create!(:email => " john@doe.com ", :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 get a blank page when I run the RSpec test. With a blank page, I mean that there is no other HTML content than a DOCTYPE declaration.

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

Interestingly, RSpec tests the work of "create" perfectly. Any clues?

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

+6
source share
2 answers

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

To fix your problem, you need to look for a response object instead of a page object.

If you want to test the content with capybara, the way to create this 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") 

This code should be placed in the request specification instead of the controller specification, by agreement.

+6
source

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

 RSpec.configure do |config| config.render_views end 

Optionally, you can call render_views in each controller specification separately.

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

+8
source

Source: https://habr.com/ru/post/909219/


All Articles