2012-02-23 73 views
6

运行以下RSpec的测试时,我得到一个空白页面的响应:运行RSpec的测试为GET时,遇到了空白页“新”

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 

运行RSpec的测试时,我只得到一个空白页。 空白页面我的意思是除DOCTYPE声明外没有其他HTML内容。

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

有趣的是,RSpec测试后'创建'工作正常。任何提示?

我正在使用Rails 3.2与spec-rails,黄瓜和水豚(而不是webrat)。

+1

我很好奇,你有没有找到了解决这个问题呢? – voxobscuro 2012-03-15 17:56:32

+0

也对此解决方案感到好奇.. – jay 2012-03-27 23:07:47

+0

不幸的是,我还没有解决方案... – 2012-04-06 16:07:31

回答

6

问题是您正在混合测试类型。通过调用visit path,提供page对象的Capybara用于请求规格。

为了解决您的问题,你需要看的response对象,而不是page对象。

如果你想测试的内容与水豚,你会建立一个测试的方式会是这个样子:

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") 

该代码应放置在要求规范,而不是一个控制器规范,由惯例。

相关问题