当预期输出正确时,Rspec has_selector错误

Car*_*yes 1 ruby-on-rails capybara rspec-rails railstutorial.org

我有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>
Run Code Online (Sandbox Code Playgroud)

与'内容'和'约'完全相同的错误

application.html.erb

<!DOCTYPE html>
<html>
<head>
    <title><%= title %></title>
    <%= csrf_meta_tag %>
</head>
<body>
    <%= yield %>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

Pau*_*nti 6

如果您正在使用Capybara 2.0,title则忽略该元素之类的隐形文本.请参阅Capybara Github有关它的问题.

Rails教程专门使用Capybara版本1.1.2,因此如果您还没有这样做,请确保根据教程Gemfile为您的所有宝石编写显式版本.

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