开发者

RSpec have_tag failing even when tag is present?

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:

&l开发者_JS百科t;!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


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?


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)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜