即使标签存在,RSpec have_tag 也会失败?
目前正在制作 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.
任何人都可以解释这里出了什么问题吗?谢谢
Currently doing the Ruby On Rails Tutorial by Michael Hartl, and am starting on test-driven development. The tutorial demands that tests be written to ensure that the right titles are present on our html.erb pages. There are three of these pages - home, contact and about. The tests look like so:
it "should have the right title" do
get 'home'
response.should have_tag("title",
"Ruby On Rails Sample Application | Home")
end
My home.html.erb file looks like so:
<!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>
As you can see, the title tag is present and it's wrapping the correct text. However, when I run my test, I get this error message:
'PagesController GET 'home' should have the right title' FAILED
Expected at least 1 element matching "title", found 0.
Can anyone explain what is going wrong here? Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你有
需要“spec_helper”
在你的pages_controller_spec.rb之上?
你有
渲染视图
你的描述块中的声明?
对于 John Paul Ashenfelter,我认为 save_and_open_page 是 Capybara 方法,而不是 Rspec?
Do you have
require 'spec_helper'
on top of your pages_controller_spec.rb?
Do you have
render_views
statement in your describe block?
To John Paul Ashenfelter, I thought save_and_open_page was Capybara method, and not Rspec?
确保页面正确呈现
save_and_open_page
您应该首先使用Rspec 中的 。通常渲染会出现问题(例如可能无效的 xHTML?)
您还需要将 launchy gem 添加到项目中(例如 Gemfile 中的 gem 'launchy')
You should start by making sure the page renders correctly using
save_and_open_page
in your Rspec. Usually there's a problem with the rendering (eg maybe invalid xHTML?)
You'll also need to add the launchy gem to your project (eg gem 'launchy' in your Gemfile)