Railstutorial.org - 集成测试打破webrat访问root_path

时间:2010-12-26 01:06:03

标签: ruby-on-rails

我正在使用railstutorial.org上的rails教程。

特别是视频。

在各种宝石版本开始崎岖不平之后 - 事情进展顺利。直到我点击“集成测试”部分。 (注意:网络教程中的第5.5节/列表5.33)

根据视频中的说明,我将其添加到layout_links_spec.rb

it "should have the right links on the layout" do 
   visit root_path
   response.should have_selector('title', :content => "Home")
end

当我运行'rspec spec /'时 - 我收到此错误

Failure/Error: response.should have_selector('title', :content => 'Home') 
expected following output to contain a <title>Home</title> tag: 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"

在添加最后一个规范之前 - 我所有的rspec测试都有效 - 也就是我'绿色'。

我当前的Gemfile包含

group :development do
  gem 'rspec-rails', '2.3.0'
end

group :test do
  gem 'rspec', '2.3.0'
  gem 'webrat', '0.7.1'
  gem 'spork', '0.8.4'
end

根据railstutorial网站的建议 - &gt; http://railstutorial.org/chapters/updating-showing-and-deleting-users#code:final_gemfile

任何帮助都将非常感激。我真的很想接受BDD / TDD - 但这些宝石'问题'真的令人沮丧。

由于 戴夫

1 个答案:

答案 0 :(得分:1)

您需要做的就是在规范的顶部添加一行来呈现视图(否则RSpec没有完整的html响应可供使用)。

require 'spec_helper'

describe PagesController do
  render_views                    # <--- This is the line you need to add!

  describe "GET 'home'" do
    it "should be successful" 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
end

这是Michael Hartl在Ruby on Rails教程书中介绍这个想法的地方:

http://railstutorial.org/chapters/static-pages#code:pages_controller_spec_title

  

“请注意,render_views行...是标题测试工作所必需的。”