标题测试未能满足Rails 3的Michael Hartls教程中的请求规范 [英] Title test failing for request specs in Michael Hartls tutorial for Rails 3

查看:61
本文介绍了标题测试未能满足Rails 3的Michael Hartls教程中的请求规范的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遵循Michael Hartl的Ruby On Rails 3教程,并使用Capybara进行集成。到目前为止的集成规范如下:

Im following the Ruby On Rails 3 tutorial by Michael Hartl and using Capybara for the integration specs. The integration specs so far are as follows

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

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

When i run these tests i get:

 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如下

I expect the title test for help and about page to fail, but my home.html.erb is as follows

<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上。是什么导致房屋的标题测试失败?

Also, i see the title 'Ruby on Rails Tutorial Sample App | Home' on '/static_pages/home'. Whats causing the title test for home to fail ?

推荐答案

Capybara 2.1更改了对查询title元素的支持。因此,以这种方式使用具有have选择器来查询html文档的标题中的title元素将失败page。shouldhave_selector('title',:text =>'Some text')

Capybara 2.1 changed its support for querying the title element. So using have selector in to query for the title element in the head of the html doc in this fashion will fail "page.should have_selector('title', :text => 'Some text').

使用 page.should have_title('Some text') 查询title元素应该起作用。这是2.1版的新方法

Use "page.should have_title('Some text')" to query the title element should work. That is new method that the 2.1 API implemented to query the title element.

此外,如果您使用capybara 2x,建议将文件移动到位于 spec文件夹中名为 requests的子文件夹中(规范/文件夹)放到名为功能(规范/功能)的新文件夹中。

Also if you using capybara 2x its suggested to move your files in the subfolder called 'requests' located in the 'spec' folder (spec/folder) into a new folder called 'features' (spec/features).

希望这可以解决。

祝您编程愉快!

这篇关于标题测试未能满足Rails 3的Michael Hartls教程中的请求规范的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆