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

2023-12-24

我正在遵循 Michael Hartl 的 Ruby On Rails 3 教程,并使用 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

当我运行这些测试时,我得到:

 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)>'

我预计帮助和关于页面的标题测试会失败,但我的 home.html.erb 如下

<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_pages/home' 上的主页'。是什么导致房屋产权测试失败?


Capybara 2.1 更改了对查询 title 元素的支持。因此,使用 have 选择器以这种方式查询 html 文档头部的 title 元素将会失败"page.should have_selector('标题', :text => '一些文本').

Use “page.should have_title('一些文本')”查询标题元素应该可以工作。这是 2.1 API 实现的用于查询 title 元素的新方法。

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

希望这能成功。

快乐编码!!

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Michael Hartls Rails 3 教程中的请求规范标题测试失败 的相关文章

随机推荐