【问题标题】:Rspec have_selector error when expected output is correct当预期输出正确时,Rspec have_selector 错误
【发布时间】:2013-01-12 06:21:09
【问题描述】:

当它们都应该成功时,我有 3 个 rspec 选择器失败了。我跟着 rails-tutorial.org 的书和他的节目是正确的。

PagesController GET 'home' should have the right title
     Failure/Error: response.should have_selector("title", :content => "Ruby on Rails     Sample App | Home")
       expected following output to contain a <title>Ruby on Rails Sample App | Home</title> tag:
   <!DOCTYPE html>
   <html>
   <head>
   <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
   <title>Ruby on Rails Tutorial Sample App | Home</title>
   </head>

“内容”和“关于”的错误完全相同

application.html.erb

<!DOCTYPE html>
<html>
<head>
    <title><%= title %></title>
    <%= csrf_meta_tag %>
</head>
<body>
    <%= yield %>
</body>
</html>

home.html.erb

    <h1>Sample App</h1>
    <p>
    This is the home page for the <a href='http://railstutorial.org'>Ruby on Rails Tutorial</a> sample application
    </p>

application_helper.erb

module ApplicationHelper

#Return a title on a per-page basis
def title
    base_title = "Ruby on Rails Tutorial Sample App"
    if @title.nil?
        base_title
    else
        "#{base_title} | #{@title}"
        end
    end
end

pages_controller_spec.rb

require 'spec_helper'

describe PagesController do
  render_views

  describe "GET 'home'" do
    it "should be successful" do
      get 'home'
      response.should be_success
    end

    it "should have the right title" do
      get 'home'
      response.should have_selector("title", :content => "Ruby on Rails Sample App |     Home")
    end

    it "should have a non-blank body" do
      get 'home'
      response.body.should_not =~ /<body>\s*<\/body>/
    end
  end

【问题讨论】:

  • 您的 Gemfile 说您使用的是什么版本的 Capybara。如果您使用的是&gt;= 2.0 版本,您将无法看到title 属性,因此为了教程的缘故,将版本设置为1.1.2。

标签: ruby-on-rails capybara rspec-rails railstutorial.org


【解决方案1】:

如果您使用 Capybara 2.0,则会忽略像 title 元素这样的不可见文本。请参阅 Capybara Github issue here。

Rails 教程专门使用 Capybara 版本 1.1.2,所以如果您还没有这样做,请确保按照 the tutorial Gemfile 为所有 gem 编写显式版本。

如果您想在现在或将来使用 Capybara 2.0,请参阅以下 SO 问题以获取帮助设置它,以及让 title 元素再次工作的测试:

【讨论】:

    【解决方案2】:

    在 Capybara 2.x 中,为了查找/与不可见文本交互,您可以传递 ":visible => false"。

    例子:

    page.should have_selector("head title", text: "my title", visible: false)
    

    这适用于所有内容,而不仅仅是标题。因此,虽然有一个 has_title() 匹配器专门用于标题,但如果您需要以其他方式与页面的隐藏内容交互,请使用 ":visible => false"。

    page.should have_selector("link href='style.css'", visible: false)
    

    这也适用于 Capybara 命令,例如 fill_in()、click_button() 等。

    click_button("hidden_button_name", visible: false)
    
    fill_in("hidden_field_name", with: "foo", visible: false)
    

    使用 Capybara 2.2.0 和 Rspec-Rails 2.14.0 确认。

    与隐藏元素的交互可能表明您的规范做得比他们应该做的更多,但这是一个单独的问题。如果您在升级 Capybara 后发现您的规范有问题,将 ":visible => false" 传递给失败的规范可能有助于您的构建绿色。

    附言如果您使用的是 Ruby 1.9+,则上述哈希语法有效。如果您仍然在 1.8+,请使用 ":visible => false"、":with => 'foo'" 等。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多