运行 RSpec 测试获取“新”时出现空白页

发布于 2025-01-08 13:20:07 字数 1629 浏览 1 评论 0原文

运行以下 RSpec 测试时,我得到一个空页面作为响应:

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、cucumber 和 capybara(而不是 webrat)。

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

煞人兵器 2025-01-15 13:20:07

我可以通过在我的spec_helper.rb 文件中添加以下内容来解决这个问题:

RSpec.configure do |config|
  config.render_views
end

您可以选择在每个控制器规范中单独调用render_views

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

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

心清如水 2025-01-15 13:20:07

问题是你混合了不同类型的测试。 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")

按照惯例,该代码应放置在请求规范而不是控制器规范中。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文