【发布时间】:2012-02-22 04:20:00
【问题描述】:
我是 TDD 测试驱动开发的新手,我正在尝试做我的前几个测试,即使我知道标题清楚地显示在页面上,我的测试也失败了
response.should have_selector("title", :content => "Ruby on Rails | Home")
任何想法我做错了什么和/或在线某处是否有 rspec 助手列表。这是一个 rails 3.2.1 应用程序顺便说一句
更新测试失败并出现此错误
Failures:
1) PagesController GET 'home' should have the right title
Failure/Error: response.should have_selector("title", :content => "Ruby on Rails | Home")
expected following output to contain a <title>Ruby on Rails | Home</title> tag:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
# ./spec/controllers/pages_controller_spec.rb:13:in `block (3 levels) in <top (required)>'
Finished in 0.17154 seconds
4 examples, 1 failure
Failed examples:
rspec ./spec/controllers/pages_controller_spec.rb:11 # PagesController GET 'home' should have the right title
我的 application.html.haml 是
!!! Strict
%html{ :xmlns => "http://www.w3.org/1999/xhtml", 'xml:lang'=>"en", :lang=>"en" }
%head
%title= "Ruby on Rails | Home"
= stylesheet_link_tag "application", :media => "all"
= javascript_include_tag "application"
= csrf_meta_tags
%body
= yield
1) PagesController GET 'home' should have the right title
Failure/Error: page.should have_selector("title", :content => "Ruby on Rails | Home")
NameError:
undefined local variable or method `page' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1:0x000001011b22e8>
# ./spec/controllers/pages_controller_spec.rb:13:in `block (3 levels) in <top (required)>'
Finished in 0.16401 seconds
4 examples, 1 failure
Failed examples:
rspec ./spec/controllers/pages_controller_spec.rb:11 # PagesController GET 'home' should have the right title
更新
这是我的规范控制器,除了有问题的测试之外,所有测试都通过了
require 'spec_helper'
describe PagesController do
describe "GET 'home'" do
subject { page }
it "returns http success" do
get 'home'
response.should be_success
end
it "should have the right title" do
get 'home'
page.should have_selector("title", :content => "Ruby on Rails | Home")
end
it "should render template" do
get 'home'
response.should render_template('pages/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
又一次更新...这是我在页面上查看源代码时的源代码
<!DOCTYPE html>
<html lang='en' xml:lang='en' xmlns='http://www.w3.org/1999/xhtml'>
<head>
<title>Ruby on Rails | Home</title>
<link href="/assets/application.css?body=1" media="all" rel="stylesheet" type="text/css" />
<link href="/assets/pages.css?body=1" media="all" rel="stylesheet" type="text/css" />
<script src="/assets/jquery.js?body=1" type="text/javascript"></script>
<script src="/assets/jquery_ujs.js?body=1" type="text/javascript"></script>
<script src="/assets/pages.js?body=1" type="text/javascript"></script>
<script src="/assets/application.js?body=1" type="text/javascript"></script>
<meta content="authenticity_token" name="csrf-param" />
<meta content="fi3Er/VJrzhpb5FPAcT0DYT9RFNu1oW3p4og5dg+jFc=" name="csrf-token" />
</head>
<body>
<h1>Pages#home</h1>
<p>
Find me in app/views/pages/home.html
</p>
</body>
</html>
是的,另一个更新
gem 'rails', '3.2.1'
gem 'mysql2'
# Gems used only for assets and not required
# in production environments by default.
group :assets do
gem 'sass-rails', '~> 3.2.3'
gem 'coffee-rails', '~> 3.2.1'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer'
gem 'uglifier', '>= 1.0.3'
end
group :development do
gem 'rspec-rails', '2.8.1'
end
group :test do
gem 'rspec', '2.8.0'
gem 'webrat', '0.7.3'
end
gem 'jquery-rails'
gem "haml", "~> 3.1.4"
【问题讨论】:
-
为什么这个测试失败了?你得到什么错误?
-
请显示错误。你的 Gemfile 中有
webrat吗? -
@nathanvda - 用失败的测试更新了我的问题
-
@KleberS。 - 用失败的测试更新了我的问题
-
@Tamer 好的,现在请粘贴您的
views/pages/index.html.erb内容文件。
标签: ruby-on-rails ruby ruby-on-rails-3 tdd automated-tests