这个Rspec测试有什么问题

时间:2012-02-22 04:20:01

标签: ruby-on-rails ruby ruby-on-rails-3 tdd automated-tests

我是TDD测试驱动开发的新手,我正在尝试进行我的前几次测试,即使我知道标题明显在页面上,我也测试失败

  response.should have_selector("title", :content => "Ruby on Rails | Home")

任何想法我做错了和/或是否有某个在线的rspec助手列表。这是rails 3.2.1 application btw

更新测试失败,出现此错误

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"

2 个答案:

答案 0 :(得分:2)

在你的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

另外,请确保在规范文件中包含render_views

describe PagesController do
  render_views
.
.
end

如果这不起作用。尝试使用不同的Rspec宝石版本 - RSpec 2.0.0.beta.19中有一个错误

group :development do
  gem 'rspec-rails', '2.0.0.beta.18'
end

group :test do
  gem 'rspec-rails', '2.0.0.beta.18'
  gem 'spork', '0.8.4'
end

答案 1 :(得分:1)

正如我所看到的,你正在使用rails教程电子书练习,所以,根据你安装的rspec和ruby版本,以下内容对你来说应该和我一样(我实际上是这样的)此刻也在运行rails教程!)

尝试将content替换为text,如下所示:

response.should have_selector("title", text: "Ruby on Rails | Home")

以上这条线对我来说很好。

另外,我注意到您使用的是response,您可能希望尝试使用page

最终结果将是:

page.should have_selector("title", :content => "Ruby on Rails | Home")

希望它对你有所帮助。

修改

确保您已在规范文件的第一行声明pagesubject

例如:

require 'spec_helper'

describe "StaticPages" do

  subject { page }
  .
  .
end

是另一个更新......这次是我的Gemfile

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的相关部分。

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

group :test do
  gem 'rspec-rails', '2.6.1'
  .
  .
end