have_selector 在 RSpec 测试中失败,但页面渲染正确并且标签存在

发布于 2024-10-21 21:31:07 字数 1562 浏览 1 评论 0原文

我正在阅读 Hartl 编写的《Rails 教程》一书,但我完全陷入其中一项测试中。测试(直接来自书中)非常简单:

require 'spec_helper'
describe UsersController do
render_views
describe "GET 'show'" do
    before(:each) do
        @user = Factory(:user)
    end
    ...
    it "should include the user's name" do
        get :show, :id => @user
        response.should have_selector('h1', :content => @user.name)
    end
end

测试失败并出现以下错误:

UsersController GET 'show' should include the user's name 失败/错误:response.should have_selector('h1', :content => @user.name) 预期以下输出包含

Steve T

tag:...

页面在浏览器中正确呈现。以下是浏览器呈现的 HTML 源代码的片段:

​​

Steve T/> 史蒂夫·T

我什至可以在控制器规范中添加对: print response.body 的调用,它将正确输出标签。

关于我可能做错了什么有什么想法吗?

更新 - 看来我们已经确定了一个潜在的重要项目:rspec 测试失败消息中的 HTML 缺少正文标签中的所有内容。

1) UsersController GET 'show' 应包含用户名 失败/错误:response.should have_selector('h1', :content => @user.name) 预期以下输出包含

Steve T< ;/h1>标签:<!DOCTYPE html>Ruby on Rails 教程示例应用程序 |史蒂夫·T# ./spec/controllers/users_controller_spec.rb:33:in block (3 level) in'

有谁知道为什么,如果浏览器中的页面包含 < ;body> 标签和内部内容,如果 print response.body 显示内容,为什么错误消息会跳过它?

I'm working my way through the Rails Tutorial book by Hartl and I'm completely stuck on one of the tests. The test (right from the book) is very simple:

require 'spec_helper'
describe UsersController do
render_views
describe "GET 'show'" do
    before(:each) do
        @user = Factory(:user)
    end
    ...
    it "should include the user's name" do
        get :show, :id => @user
        response.should have_selector('h1', :content => @user.name)
    end
end

The test fails with the following error:

UsersController GET 'show' should include the user's name
Failure/Error: response.should have_selector('h1', :content => @user.name)
expected following output to contain a <h1>Steve T</h1> tag:...

The page renders properly in the browser. Here is a snipped from the HTML source rendered by the browser:

<section class="round">
<h1>
<img alt="Steve T" class="gravatar" src="http://gravatar.com/..." />
Steve T
</h1>
</section>

I can even add a call to: print response.body in the controller spec and it will output the tags properly.

Any ideas on what I may be doing wrong?

UPDATE - It seems like we have identified a potentially significant item: The HTML in the rspec test failure message is missing the body tag everything inside it.

1) UsersController GET 'show' should include the user's name Failure/Error: response.should have_selector('h1', :content => @user.name) expected following output to contain a <h1>Steve T</h1> tag: <!DOCTYPE html> <html><head> <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> <title>Ruby on Rails Tutorial Sample App | Steve T</title> </head></html> # ./spec/controllers/users_controller_spec.rb:33:in block (3 levels) in <top (required)>'

Does anyone know why, if the page in the browser includes the <body> tags and the inner content and if print response.body shows the content, why would the error message skip it?

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

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

发布评论

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

评论(4

看春风乍起 2024-10-28 21:31:07

我在学习 Rails 教程时遇到了类似的问题。我忘记在顶部“描述”语句下方包含“render_views”,这导致“have_selector”测试失败。

I had a similar problem when working through the Rails Tutorial. I forgot to include 'render_views' below the top 'describe' statement, which was causing the 'have_selector' test to fail.

巨坚强 2024-10-28 21:31:07

我的解决方案是安装 capybara
包含在您的 Gemfile 中

group :development do
 gem 'capybara'
end

,然后

bundle update
bundle install

编辑代码以

response.body.should have_selector('h1', :content => @user.name)

运行测试。应该有效

My solution was in installing capybara,
include in your Gemfile

group :development do
 gem 'capybara'
end

then do

bundle update
bundle install

than edit you code to

response.body.should have_selector('h1', :content => @user.name)

now run your tests. It should work

云归处 2024-10-28 21:31:07

测试失败,因为 h1 包裹在 周围,因此内容不正确,测试失败。

你可以这样做:

it "should include the user's name" do
    get :show, :id => @user
    response.should have_selector('h1')
    response.body.should contain(@user.name);
end

The test is failing because the h1 is wrapped around a <img /> so the content isn't correct and the test fails.

you could do something like this:

it "should include the user's name" do
    get :show, :id => @user
    response.should have_selector('h1')
    response.body.should contain(@user.name);
end
天涯离梦残月幽梦 2024-10-28 21:31:07

事实证明,加载样式表的部分中的语法错误本质上是注释掉了所有正文标记内容。问题是为什么 Firefox 3 无论如何都要渲染 HTML。实际上,我升级到 Firefox 4 无意中解决了这个问题。在 Firefox 4 中,页面正文未呈现,调试工具导致我出现错误的标记

It turns out that syntax mistake in the partial that loads my style sheets was essentially commenting out all the body tag content. The question is why Firefox 3 rendered the HTML anyway. I actually solved the problem inadvertently by upgrading to Firefox 4. In Firefox 4, the body of the page didn't render and the debugging tools led me to the erroneous markup

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