【问题标题】:The have_selector fails in an RSpec test but page renders correctly and the tag is presenthave_selector 在 RSpec 测试中失败,但页面正确呈现并且标签存在
【发布时间】:2013-08-13 22:13:56
【问题描述】:

我正在阅读 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 Failure/Error: response.should have_selector('h1', :content => @user.name) expected following output to contain a <h1>Steve T</h1> tag:...

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

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

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

关于我可能做错的任何想法?

更新 - 似乎我们已经确定了一个潜在的重要项目:rspec 测试失败消息中的 HTML 缺少其中的正文标记。

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

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

【问题讨论】:

  • 请注意,但我认为 have_selector 完全匹配?在您的情况下,H1 包含嵌入式 IMG。我想如果那个 IMG 不在那里,它会起作用。试过了吗?我相信have_selector 是一个 webrat 匹配器。你可能想试试response.should contain(@user.name)。或者这个可能工作:response.should have_selector('h1 img', :content =&gt; @user.name).
  • 据我所知,这正是正在发生的事情。

标签: ruby-on-rails rspec webrat


【解决方案1】:

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

【讨论】:

  • 这是一个很好的检查,它让我绊倒了好几次
【解决方案2】:

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

group :development do
 gem 'capybara'
end

然后做

bundle update
bundle install

比把你的代码编辑成

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

现在运行您的测试。它应该工作

【讨论】:

    【解决方案3】:

    测试失败,因为 h1 包裹在 &lt;img /&gt; 周围,因此内容不正确,测试失败。

    你可以这样做:

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

    【讨论】:

    • 感谢您的快速回复。我修改了视图文件以将图像和名称放在单独的 h1 标记中。仍然得到同样的错误。现在浏览器中呈现的代码是:&lt;h1&gt;&lt;img alt="Steve T" class="gravatar" src="http://gravatar.com/avatar/...?size=50" /&gt;&lt;/h1&gt; &lt;h1&gt;Steve T&lt;/h1&gt;
    • 这是因为所有h1s内容都应该是@user.name,有什么理由让&lt;img&gt;出现在&lt;h1&gt;中吗?
    • 我刚刚再次修改了视图以从 h1.xml 中删除图像。仍然没有运气。这是 HTML:&lt;img alt="Steve T" class="gravatar" src="http://gravatar.com/..." /&gt; &lt;h1&gt;Steve T&lt;/h1&gt; 再次感谢。非常感谢您的帮助。
    • 既然你已经完成了,你可以再次使用response.should have_selector('h1', :content =&gt; @user.name)
    • 它给出了同样的错误。我使用以下代码将响应和@user.name 转储到终端,以确保变量已正确初始化。标签位于 html 中,并且名称在星号之间正确打印。 it "should include the user's name" do get :show, :id =&gt; @user print response.body print '***' print @user.name print '***' response.should have_selector('h1', :content =&gt; @user.name) end
    【解决方案4】:

    事实证明,加载我的样式表的部分语法错误实际上是注释掉了所有正文标记内容。问题是为什么 Firefox 3 仍然呈现 HTML。我实际上是通过升级到 Firefox 4 无意中解决了这个问题。在 Firefox 4 中,页面的主体没有呈现,并且调试工具导致我出现错误的标记

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-25
      • 1970-01-01
      • 1970-01-01
      • 2011-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-15
      相关资源
      最近更新 更多