Rspec失败了4个例子,1个失败

时间:2013-02-02 05:24:11

标签: ruby-on-rails ruby rspec

我一直在跟随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>

1 个答案:

答案 0 :(得分:1)

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

我尝试了很多变体和替代品,并得出结论,最新的Capybara仍然存在一些兼容性问题。

希望这有帮助。

  

更新:诅咒它。从您的网站查看主页的来源   浏览器。由于应用程序布局使用<body>内的产量,所有你   home.html.erb中的html实际上是在体内替换的(即使是   有效地,<title>部分内的<body>部分被替换   取消<head><title>个元素。这就是没有观点的原因   具体名称。标题定义于   /views/layout/application.html.erb正在您的主页上使用。

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