RSpec失败:“静态页面X应该有标题X” - 尝试使用have_xpath& have_selector

时间:2012-12-16 02:04:57

标签: ruby-on-rails rspec railstutorial.org

我一直在搜索网络& Stack Overflow to tr并弄清楚如何使这些测试工作。我是Ruby&的新手。对于Rails,我只是按照Hartl的教程进行操作 - 复制粘贴大部分代码,看看它们最终是如何组合在一起的。

现在,我陷入第3.3节“轻微动态页面”。

这是我收到的错误:

C:\Sites\sample_app>bundle exec rspec --no-color spec/requests/static_pages_spec.rb
F.F.F.

Failures:

  1) Static pages About page should have the title 'About Us'
 Failure/Error: page.should have_selector('title',
   expected css "title" with text "Ruby on Rails Tutorial Sample App | About Us" to return something
 # ./spec/requests/static_pages_spec.rb:44:in `block (3 levels) in <top (required)>'

  2) Static pages Help page should have the title 'Help'
 Failure/Error: page.should have_selector('title',
   expected css "title" with text "Ruby on Rails Tutorial Sample App | Help" to return something
 # ./spec/requests/static_pages_spec.rb:29:in `block (3 levels) in <top (required)>'

  3) Static pages Home page should have the title 'Home'
 Failure/Error: page.should have_selector('title',
   expected css "title" with text "Ruby on Rails Tutorial Sample App | Home" to return something
 # ./spec/requests/static_pages_spec.rb:14:in `block (3 levels) in <top (required)>'

Finished in 3.09 seconds
6 examples, 3 failures

Failed examples:

rspec ./spec/requests/static_pages_spec.rb:42 # Static pages About page should have the title 'About Us'
rspec ./spec/requests/static_pages_spec.rb:27 # Static pages Help page should have the title 'Help'
rspec ./spec/requests/static_pages_spec.rb:12 # Static pages Home page should have the title 'Home'

Randomized with seed 25648

一旦我从这个HTML结构中切换(在我的About / Home / Help.html.erb文件中),就会出现错误:

<% provide(:title, 'Home') %>
<!DOCTYPE html>
<html>
  <head>
    <title>Ruby on Rails Tutorial Sample App | <%= yield(:title) %></title>
  </head>
  <body>
  <h1>Sample App</h1>
    <p>
      This is the home page for the
      <a href="http://railstutorial.org/">Ruby on Rails Tutorial</a>
      sample application.
    </p>
</body>
</html>

对此:

<% provide(:title, 'Home') %>
<h1>Sample App</h1>
  <p>
     This is the home page for the
     <a href="http://railstutorial.org/">Ruby on Rails Tutorial</a>
     sample application.
  </p>

其他相关文件:

Application.html.erb:

<!DOCTYPE html>
<html>
  <head>
    <title>Ruby on Rails Tutorial Sample App | <%= yield(:title) %></title>
    <%= stylesheet_link_tag    "application", :media => "all" %>
    <%= javascript_include_tag "application" %>
    <%= csrf_meta_tags %>
  </head>
  <body>
    <%= yield %>
  </body>
</html>

static_pages_spec.rb

require 'spec_helper'

describe "Static pages" 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")

     // The line below is something I tried replacing the page.should have_selector with
    // page.should have_xpath("//title", :text => "Home")
  end
end

.... (other describe pages, same structure)

end

我可能只是对Hartl的教程中的复制/粘贴/阅读过程视而不见,但我非常确定它看起来和他描述的一样。

我已经尽力寻找解决方案,但是唉,我还没弄清楚,所以这里就行了!

干杯!

编辑:回答Fiona#1

在此网址:“http:// localhost:3000 / static_pages / home”

文档标题不算什么。

该文件的来源如下:

<h1>Sample App</h1>
<p>
  This is the home page for the
  <a href="http://railstutorial.org/">Ruby on Rails Tutorial</a>
  sample application.
</p>

源中没有doctype,head,body或title声明。

1 个答案:

答案 0 :(得分:4)

要让Rails选择布局文件,application.html.erb文件应位于app / views / layouts / application.html.erb中。