运行 RSpec 测试获取“新”时出现空白页
运行以下 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我可以通过在我的spec_helper.rb 文件中添加以下内容来解决这个问题:
您可以选择在每个控制器规范中单独调用
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:
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
问题是你混合了不同类型的测试。 Capybara 提供了页面对象,通过调用访问路径在请求规范中使用。
为了解决您的问题,您需要查看
response
对象而不是page
对象。如果您想使用水豚测试内容,构建该测试的方式将如下所示:
按照惯例,该代码应放置在请求规范而不是控制器规范中。
The problem is that you are mixing types of tests. Capybara, which provides the
page
object is used in request specs by callingvisit path
.In order to fix your problem, you need to be looking at the
response
object instead of thepage
object.If you want to test content with capybara, the way that you would build that test would look something like this:
That code should be placed in a request spec instead of a controller spec, by convention.