职称考试失败对迈克尔Hartls教程要求规格为Rails 3(Title test failing

2019-08-02 23:54发布

林继Ruby on Rails的3教程由迈克尔·哈特尔和使用水豚为一体的规格。 整合规范至今如下

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 /家' 主页。 什么导致产品失败的职称考试?

Answer 1:

水豚2.1改变了查询标题元素的支持。 因此,使用在有选择查询在以这种方式在HTML文档的头部标题元素会失败“page.should have_selector(‘标题’,:文本=>‘一些文本’)。

使用“page.should have_title(‘一些文本’)”查询标题元素应该工作。 这是2.1 API实现查询标题元素新方法。

此外,如果您使用的是水豚2X其建议中移动称为“请求”位于“规范”文件夹(规格/文件夹)到一个名为“功能”(规格/功能)的新文件夹的子文件夹文件。

希望这个作品出来。

编码愉快!



Answer 2:

在你的Gemfile,变化

gem 'capybara'

gem 'capybara', '1.1.2'

并运行“捆绑更新”。



文章来源: Title test failing for request specs in Michael Hartls tutorial for Rails 3