即使标签存在,RSpec has_tag也会失败?

时间:2011-05-16 19:12:55

标签: ruby-on-rails ruby rspec

目前正在进行Michael Hartl的Ruby On Rails教程,并开始进行测试驱动开发。本教程要求编写测试以确保我们的html.erb页面上存在正确的标题。这些页面有三个 - 家庭,联系和约。测试看起来像这样:

it "should have the right title" do
  get 'home' 
  response.should have_tag("title",
                         "Ruby On Rails Sample Application | Home")
end

我的home.html.erb文件如下所示:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>Ruby On Rails Sample Application | Home</title>
</head>
<body>
<h1>Sample App Home</h1>
<p>This is the home page for the 
<a href="http://www.railstutorial.org/">Ruby on Rails Tutorial</a>
sample application.</p>
</body>
</html>

正如您所看到的,标题标签存在并且正在包装正确的文本。但是,当我运行我的测试时,我收到此错误消息:

'PagesController GET 'home' should have the right title' FAILED
 Expected at least 1 element matching "title", found 0.

有谁可以解释这里出了什么问题?感谢

2 个答案:

答案 0 :(得分:1)

你有吗? require 'spec_helper' 在您的pages_controller_spec.rb?

之上 你有吗? render_views 你的描述块中的陈述?

对于John Paul Ashenfelter,我以为save_and_open_page是Capybara方法,而不是Rspec?

答案 1 :(得分:0)

首先应确保使用

正确呈现页面

save_and_open_page

在你的Rspec中。通常渲染有问题(例如,可能是无效的xHTML?)

您还需要将launchy gem添加到您的项目中(例如Gemfile中的gem'statey')

相关问题