使用 rspec 验证页面标题 [英] Verifying page title with rspec

查看:37
本文介绍了使用 rspec 验证页面标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读 Michael Hartl 的 Rails 教程.

I'm running through Michael Hartl's Rails Tutorial.

我正在尝试验证我的页面的标题.测试如下:

I'm trying to verify the title of my page. The test looks like this:

it "should have the right title" do
      get 'home'
      response.should have_selector("title", :content => "Ruby on Rails Tutorial Sample App | Home")
    end

HTML 头部部分看起来像这样

The HTML head section looks like this

<head>
    <title>Ruby on Rails Tutorial Sample App | Home</title>
</head>

我遇到以下失败

1) PagesController GET 'home' 应该有正确的标题失败/错误: response.should have_selector("title", :content =>Ruby on Rails 教程示例应用程序 |家")预期以下输出包含 Ruby on Rails教程示例应用程序 |家标签:# ./spec/controllers/pages_controller_spec.rb:13:in`块(3 级)在 '

1) PagesController GET 'home' should have the right title Failure/Error: response.should have_selector("title", :content => "Ruby on Rails Tutorial Sample App | Home") expected following output to contain a Ruby on Rails Tutorial Sample App | Home tag: # ./spec/controllers/pages_controller_spec.rb:13:in `block (3 levels) in '

我期待这会通过.我究竟做错了什么?我正在使用 Rails 3 和 RSpec 2.0.0

I'm expecting this to pass. What am I doing wrong? I'm using Rails 3 and RSpec 2.0.0

推荐答案

控制器规范通常不会呈现完整的视图,因为它们旨在单独测试控制器.您可以通过在示例组的顶部包含指令 integrate_views 来告诉 Rspec 呈现整个页面:

Controller specs don't normally render the complete view, since they're intended to test controllers in isolation. You can tell Rspec to render the whole page by including the directive integrate_views at the top of the example group:

describe MyController do
  integrate_views

但是,您应该问问自己是否真的想这样做,或者编写视图规范是否更有意义.

However you should ask yourself if you really want to do this, or if it would make more sense to write view specs.

顺便说一句,您还可以使用 CSS3 选择器语法来节省一些按键操作(可能需要为此包含 Webrat 匹配器):

btw you can also use the CSS3 selector syntax to save a few keystrokes (might need to include the Webrat matchers for this):

response.should have_selector("title:contains('Ruby on Rails Tutorial Sample App | Home')")

编辑

对于 Rspec2,将 integrate_views 替换为 render_views

For Rspec2, replace integrate_views with render_views

这篇关于使用 rspec 验证页面标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆