Ruby on rails 3 Rails 3的Michael Hartls教程中请求规范的标题测试失败

Ruby on rails 3 Rails 3的Michael Hartls教程中请求规范的标题测试失败,ruby-on-rails-3,integration-testing,capybara,Ruby On Rails 3,Integration Testing,Capybara,我正在学习MichaelHartl的RubyonRails3教程,并使用Capybara作为集成规范。目前的集成规范如下 require 'spec_helper' describe "StaticPages" do describe "Home page" do it "should have the h1 'Sample App'" do visit '/static_pages/home' page.should have_selector('h1',

我正在学习MichaelHartl的RubyonRails3教程,并使用Capybara作为集成规范。目前的集成规范如下

require 'spec_helper'

describe "StaticPages" do
  describe "Home page" do
    it "should have the h1 'Sample App'" do
      visit '/static_pages/home'
      page.should have_selector('h1',:text => 'Sample App')
    end

    it "should have the title 'Home'" do
      visit '/static_pages/home'
      page.should have_selector('title',:text => "Ruby on Rails Tutorial Sample App | Home")
    end
  end

  describe "Help page" do
    it "should have the h1 'Help'" do
      visit '/static_pages/help'
      page.should have_selector('h1',:text => 'Help')
    end

    it "should have the title 'Help'" do
      visit '/static_pages/help'
      page.should have_selector('title',:text => "Ruby on Rails Tutorial Sample App | Help")
    end
  end

  describe "About page" do
    it "should have the h1 'About Us'" do
      visit '/static_pages/about'
      page.should have_selector('h1',:text => 'About Us')
    end

    it "should have the title 'About'" do
      visit '/static_pages/about'
      page.should have_selector('title',:text => "Ruby on Rails Tutorial Sample App | About Us")
    end
  end
end
<html>
<head>
  <title>Ruby on Rails Tutorial Sample App | Home</title>
</head>
<body>
<h1>Sample App</h1>
<p>
This is the homepage for the sample app
</p>
</body>
</html>
当我运行这些测试时,我得到:

 1) StaticPages Home page should have the title 'Home'
     Failure/Error: page.should have_selector('title',:text => "Ruby on Rails Tutorial Sample App | Home")
       expected #has_selector?("title") to return true, got false
     # ./spec/requests/static_pages_spec.rb:12:in `block (3 levels) in <top (required)>'

  2) StaticPages Help page should have the title 'Help'
     Failure/Error: page.should have_selector('title',:text => "Ruby on Rails Tutorial Sample App | Help")
       expected #has_selector?("title") to return true, got false
     # ./spec/requests/static_pages_spec.rb:24:in `block (3 levels) in <top (required)>'

  3) StaticPages About page should have the title 'About'
     Failure/Error: page.should have_selector('title',:text => "Ruby on Rails Tutorial Sample App | About Us")
       expected #has_selector?("title") to return true, got false
     # ./spec/requests/static_pages_spec.rb:36:in `block (3 levels) in <top (required)>'
1)StaticPages主页的标题应为“主页”
失败/错误:page.should应具有_选择器('title',:text=>“RubyonRails教程示例应用程序| Home”)
预期#has#u selector?(“title”)返回true,但返回false
#./spec/requests/static\u pages\u spec.rb:12:in'block(3层)in'
2) StaticPages帮助页面的标题应为“帮助”
失败/错误:page.should应具有_选择器('title',:text=>“RubyonRails教程示例应用程序|帮助”)
预期#has#u selector?(“title”)返回true,但返回false
#./spec/requests/static\u pages\u spec.rb:24:in'block(3层)in'
3) 关于页面的静态页面标题应为“关于”
失败/错误:page.should有_选择器('title',:text=>“RubyonRails教程示例应用程序|关于我们”)
预期#has#u selector?(“title”)返回true,但返回false
#./spec/requests/static\u pages\u spec.rb:36:in'block(3层)in'
我希望帮助和关于页面的标题测试失败,但是我的home.html.erb如下

require 'spec_helper'

describe "StaticPages" do
  describe "Home page" do
    it "should have the h1 'Sample App'" do
      visit '/static_pages/home'
      page.should have_selector('h1',:text => 'Sample App')
    end

    it "should have the title 'Home'" do
      visit '/static_pages/home'
      page.should have_selector('title',:text => "Ruby on Rails Tutorial Sample App | Home")
    end
  end

  describe "Help page" do
    it "should have the h1 'Help'" do
      visit '/static_pages/help'
      page.should have_selector('h1',:text => 'Help')
    end

    it "should have the title 'Help'" do
      visit '/static_pages/help'
      page.should have_selector('title',:text => "Ruby on Rails Tutorial Sample App | Help")
    end
  end

  describe "About page" do
    it "should have the h1 'About Us'" do
      visit '/static_pages/about'
      page.should have_selector('h1',:text => 'About Us')
    end

    it "should have the title 'About'" do
      visit '/static_pages/about'
      page.should have_selector('title',:text => "Ruby on Rails Tutorial Sample App | About Us")
    end
  end
end
<html>
<head>
  <title>Ruby on Rails Tutorial Sample App | Home</title>
</head>
<body>
<h1>Sample App</h1>
<p>
This is the homepage for the sample app
</p>
</body>
</html>

Ruby on Rails教程示例应用程序|主页
示例应用程序

这是示例应用程序的主页


另外,我在/static\u pages/Home上看到了标题“RubyonRails教程示例应用程序| Home”。是什么导致home的标题测试失败?

在GEM文件中,更改

gem 'capybara'
进入


并运行“bundle update”。

Capybara 2.1更改了对查询title元素的支持。因此,使用have selector in以这种方式查询html文档头部的title元素将失败“page.should_selector('title',:text=>'Some text')

使用“page.should have_title('Some text')”查询title元素应该有效。这是2.1 API实现的查询title元素的新方法

此外,如果您使用capybara 2x,建议将“spec”文件夹(spec/folder)中名为“requests”的子文件夹中的文件移动到名为“features”(spec/features)的新文件夹中

希望这能奏效


愉快的编码!!

谢谢。如果有人使用水豚2,会如何实现同样的效果?请尝试
page。应该有选择器('title',:content=>'Sample App')
(:text已替换为:content。)这可能也会解决原来的问题。感谢您花时间回答评论。不幸的是,对于capybara 2.0.2,我得到了ArgumentError:无效键:内容,应该是以下其中之一:text、:visible、:between、:count、:max、:minimumplease mark Torstein的答案作为可接受的答案。