使用rspec验证页面标题

Jac*_*ius 9 rspec ruby-on-rails

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

我正在尝试验证我的页面标题.测试看起来像这样:

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

HTML头部分看起来像这样

<head>
    <title>Ruby on Rails Tutorial Sample App | Home</title>
</head>
Run Code Online (Sandbox Code Playgroud)

我遇到了以下失败

1)PagesController GET'home'应该有正确的标题失败/错误:response.should have_selector("title",:content =>"Ruby on Rails Tutorial Sample App | Home")预期输出后包含Ruby on Rails教程示例应用程序| 主页标签:#./ spec/control/pages_controller_spec.rb:13:在'块(3级)中'

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

zet*_*tic 5

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

describe MyController do
  integrate_views
Run Code Online (Sandbox Code Playgroud)

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

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

response.should have_selector("title:contains('Ruby on Rails Tutorial Sample App | Home')")
Run Code Online (Sandbox Code Playgroud)

编辑

对于 Rspec2,替换integrate_viewsrender_views

  • 糟糕,你必须使用 Rspec 2。试试 `render_views`。 (2认同)