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

标签 ruby-on-rails-3 integration-testing capybara

我正在学习 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 selector in 查询 html 文档头部的 title 元素将失败 "page.should have_selector('title', :text => 'Some text')

使用 "page.should have_title('Some text')" 查询标题元素是否有效。这是 2.1 API 实现的查询 title 元素的新方法。

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

希望一切顺利。

编码愉快!!

关于ruby-on-rails-3 - Rails 3 的 Michael Hartls 教程中的请求规范的标题测试失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13479601/

相关文章:

ruby-on-rails - 如何在ActiveRecord查询中使用OR条件

ruby-on-rails - 命名空间 form_for 中的嵌套资源

php - 使用 Simpletest 对 CodeIgniter 进行单元测试 - 很少的测试

java - 从 intellij 运行时,Spring 初始化不同

ruby - capybara 找不到要附加文件的文件输入字段

mysql - 如何将现有用户表导入另一个具有不同结构的表?

ruby catch连接超时错误

authentication - 在 .NET Core API 集成测试中使用正确的身份验证方案

ruby-on-rails - 使用 Rspec + Capybara + Ember 出现不一致的 "Unable to find css"错误

ruby - 为什么 Capybara "wait for page to load"计时器似乎适用于匹配器而不适用于发现器?