我一直在搜索网络和 Stack Overflow 以了解如何让这些测试正常工作。我是 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>
其他相关文件:
应用程序.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 声明。