0

我一直在关注 Michael Hartl 的教程,尽管由于某种原因我在测试时似乎遇到了这个错误:

Failures:   

1) PagesController GET 'home' should have the right title
 Failure/Error: response.should have_selector("title",
   expected css "title" to return something
 # ./spec/controllers/pages_controller_spec.rb:18:in `block (3 levels) in <top (required)>'

Finished in 0.11535 seconds
4 examples, 1 failure

Failed examples:

rspec ./spec/controllers/pages_controller_spec.rb:16 # PagesController GET 'home' should have the right title

Randomized with seed 19403

这是我的 pages_controller_spec 文件内容:

require 'spec_helper'



describe PagesController do


render_views

describe "GET 'home'" do
it "returns http success" do
  get 'home'
  response.should be_success
end

it "should have the right title" do
  get 'home'
  response.should have_selector("title", 
      :content => "Ruby on rails tutorial sample app | Home")
 end
 end

describe "GET 'contact'" do
it "returns http success" do
  get 'contact'
  response.should be_success
 end
end


describe "GET 'about'" do
it "returns http success" do
  get 'about'
  response.should be_success
end
 end
 end

这是我的 home.html.erb 文件的内容:

<!DOCTYPE>
<html>
<head>
    <title>Ruby on rails tutorial sample app | Home</title>
</head>
<body>
    <h1>Pages#home</h1>
    <p>Find me in app/views/pages/home.html.erb</p>
</body>
 </html>
4

1 回答 1

1

是的,这也发生在我身上。请确保您使用的是教程中提到的 Capybara 的确切版本。即1.1.2

我尝试了许多变体和替代方案,得出的结论是,最新的 Capybara 中仍然存在一些兼容性问题。

希望这可以帮助。

更新:搞定了。从浏览器检查主页的来源。由于应用程序布局使用 yield inside <body>,因此 home.html.erb 中的所有 html 实际上都在 body 内部被替换(甚至 <title>部分被替换在<body>tag 内),有效地使<head>元素<title>无效。这就是为什么还没有特定视图标题的原因。/views/layout/application.html.erb 中定义的标题正在您的主页上使用。

我的建议是继续学习本教程。只需几个步骤,您就可以通过测试。

于 2013-02-02T05:29:27.130 回答