为什么Rspec说“失败/错误:无法从回溯中找到匹配的行”?

时间:2010-08-19 00:05:53

标签: ruby-on-rails ruby ruby-on-rails-3 rspec autotest

我在这里关注导轨教程:http://railstutorial.org/chapters/filling-in-the-layout#top

当我运行“rspec spec /”时,我得到一堆看起来像这样的错误:

1) LayoutLinks should have a Home page at '/'
    Failure/Error: Unable to find matching line from backtrace
    stack level too deep
    # C:/Ruby19/lib/ruby/1.9.1/forwardable.rb:185

2) LayoutLinks should have a Contact page at '/contact'
    Failure/Error: Unable to find matching line from backtrace
    stack level too deep
    # C:/Ruby19/lib/ruby/1.9.1/forwardable.rb:185

但是当我在我的网络浏览器中访问localhost:3000 /和localhost:3000 / contact时,页面就在那里并且有正确的标题。这是我的myrailsroot \ spec \ requests \ layout_links_spec.rb文件:

require 'spec_helper'

describe "LayoutLinks" do

  it "should have a Home page at '/'" do
    get '/'
    response.should have_selector('title', :content => "Home")
  end

  it "should have a Contact page at '/contact'" do
    get '/contact'
    response.should have_selector('title', :content => "Contact")
  end

  it "should have an About page at '/about'" do
    get '/about'
    response.should have_selector('title', :content => "About")
  end

  it "should have a Help page at '/help'" do
    get '/help'
    response.should have_selector('title', :content => "Help")
  end

  it "should have a signup page at '/signup'" do
    get '/signup'
    response.should have_selector('title', :content => "Sign up")
  end

end

任何想法都会很棒,谢谢

11 个答案:

答案 0 :(得分:14)

这是由于RSpec 2.0.0.beta.19中的错误。如果您按照教程的建议使用2.0.0.beta.18,它将正常工作。只需将Gemfile中的任何版本更改为beta 18,捆绑安装并再次运行测试。

以下是我的Gemfile中的相关部分。

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

另请注意,Spork也会不时出现这样的问题。如果你得到了莫名其妙的测试失败,特别是如果你刚刚添加了新的控制器或动作,那就去激励一下。按Ctrl-C并再次运行spork服务器。

答案 1 :(得分:9)

我的gemfile看起来像这样,它可以正常工作

group :test do 
    gem 'rspec-rails'
    gem 'webrat', '0.7.1'
end

其中rspec-rails(2.1.0)

然而,以下不是:

group :test do 
    gem 'rspec-rails'
    gem 'webrat', '0.7.2'
end

所以我认为这是webrat的播放。

答案 2 :(得分:3)

我已升级到beta.20,现已退出。不得不将webrat添加到我的gemfile中并进行另一个bundle install。在gemfile中,它看起来像这样:

group :test do
  gem "webrat"
  gem 'rspec', '2.0.0.beta.20'
end

干杯

答案 3 :(得分:1)

我只在两个标题测试中看到了这个问题。

我的gemfile如下......

source 'http://rubygems.org'

gem 'rails', '3.0.0'
gem 'sqlite3-ruby', '1.2.5', :require => 'sqlite3'
gem 'gravatar_image_tag', '0.1.0'
gem 'will_paginate', '3.0.pre2'

group :development do
  gem 'rspec-rails', '2.0.0.rc'
  gem 'webrat', '0.7.1'
  gem 'annotate-models', '1.0.4'
  gem 'faker', '0.3.1'
end

group :test do
  gem 'rspec', '2.0.0.rc'
  gem 'webrat', '0.7.1'
  gem 'spork', '0.8.4'
  gem 'factory_girl_rails', '1.0'
end

我也试过了测试rspec-rails的测试版,但无济于事。

仍然给我错误的两个标题如下:

来自users_controller_spec.rc

  it "should have the right title" do
    get :index
    response.should have_selector("title", :content => "All users")
  end

  #...

    it "should have the right title" do
      post :create, :user => @attr
      response.should have_selector("title", :content => "Sign up")
    end

错误的片段显示为:

Failures:
  1) UsersController GET 'index' for signed-in users should have the right title
     Failure/Error: response.should have_selector("title", :content => "All users")
     expected following output to contain a <title>All users</title> tag:
     <!DOCTYPE html>
     <html>
     <head>
     <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
     <title>Ruby on Rails Tutorial Sample App | All Users</title>

2) UsersController Post 'create' for non-signed in users failure should have the right title
     Failure/Error: response.should have_selector("title", :content => "Sign up")
     expected following output to contain a <title>Sign up</title> tag:
     <!DOCTYPE html>
     <html>
     <head>
     <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
     <title>Ruby on Rails Tutorial Sample App | Sign Up</title>

分别

正如输出所示,“注册”和“索引”清楚地显示在标题的右侧。这尤其令人困惑,以下测试确实有效:

  it "should have the right title" do
    get :new
    response.should have_selector("title", :content => "Sign up")
  end

对于同一页面,它包含与其他“注册”测试相同的标题。 get方法也适用于此测试,但不适用于“索引”测试。

帮助?

答案 4 :(得分:1)

这似乎不是 rspec 2.2.0

的问题

答案 5 :(得分:1)

我正在工作

W__A__Y__!比你需要的更多的信息,但是它的价值,这个Gemfile ......

source 'http://rubygems.org'

gem 'rails', '3.0.3'

# Bundle edge Rails instead: <br/>
# gem 'rails', :git => 'git://github.com/rails/rails.git'

gem 'sqlite3-ruby', :require => 'sqlite3'

# Use unicorn as the web server<br/>
# gem 'unicorn'

# Deploy with Capistrano<br/>
# gem 'capistrano'

# To use debugger (ruby-debug for Ruby 1.8.7+, ruby-debug19 for Ruby 1.9.2+)<br/>
# gem 'ruby-debug'<br/>
# gem 'ruby-debug19'

# Bundle the extra gems:<br/>
# gem 'bj'<br/>
# gem 'nokogiri'<br/>
# gem 'sqlite3-ruby', :require => 'sqlite3'<br/>
# gem 'aws-s3', :require => 'aws/s3'<br/>

# Bundle gems for the local environment. Make sure to<br/>
# put test-only gems in this group so their generators<br/>
# and rake tasks are available in development mode:<br/>
# group :development, :test do<br/>
#   gem 'webrat'<br/>
# end<br/>
group :test, :development do<br/>
gem 'rspec-rails', '2.0.0.beta.18'<br/>
# gem 'capybara'<br/>
gem 'webrat', '0.7.1'<br/>
gem 'database_cleaner'<br/>
gem 'cucumber-rails'<br/>
gem 'cucumber'<br/>
gem 'spork'<br/>
gem 'launchy'<br/>
end

......我跑完......

$ bundle install

......和......

$ bundle update webrat

...导致这... ... ...

Using rake (0.8.7) <br/>
Using abstract (1.0.0) <br/>
Using activesupport (3.0.3) <br/>
Using builder (2.1.2) <br/>
Using i18n (0.5.0) <br/>
Using activemodel (3.0.3) <br/>
Using erubis (2.6.6) <br/>
Using rack (1.2.1) <br/>
Using rack-mount (0.6.13) <br/>
Using rack-test (0.5.7) <br/>
Using tzinfo (0.3.23) <br/>
Using actionpack (3.0.3) <br/>
Using mime-types (1.16) <br/>
Using polyglot (0.3.1) <br/>
Using treetop (1.4.9) <br/>
Using mail (2.2.14) <br/>
Using actionmailer (3.0.3) <br/>
Using arel (2.0.7) <br/>
Using activerecord (3.0.3) <br/>
Using activeresource (3.0.3) <br/>
Using bundler (1.0.7) <br/>
Using configuration (1.2.0) <br/>
Using diff-lcs (1.1.2) <br/>
Using json (1.4.6) <br/>
Using gherkin (2.3.3) <br/>
Using term-ansicolor (1.0.5) <br/>
Using cucumber (0.10.0) <br/>
Using cucumber-rails (0.3.2) <br/>
Using database_cleaner (0.6.0) <br/>
Using launchy (0.3.7) <br/>
Using nokogiri (1.4.4) <br/>
Using thor (0.14.6) <br/>
Using railties (3.0.3) <br/>
Using rails (3.0.3) <br/>
Using rspec-core (2.4.0) <br/>
Using rspec-expectations (2.4.0) <br/>
Using rspec-mocks (2.4.0) <br/>
Using rspec (2.4.0) <br/>
Using webrat (0.7.1) <br/>
Using rspec-rails (2.0.0.beta.18) <br/>
Using spork (0.8.4) <br/>
Using sqlite3-ruby (1.3.2)

...将所有示例应用程序测试(通过第5章末尾)再次运行。

答案 6 :(得分:1)

我可以确认以下Gemfile正常工作......

group :test do
  gem 'rspec', '2.0.0.beta.18'
  gem 'spork', '0.8.4'
  gem 'webrat', '0.7.1'
end

答案 7 :(得分:0)

在我指定webrat 0.7.1之前,我仍然看到这个问题。

答案 8 :(得分:0)

以下为我解决了这个问题。

gem install rspec-rails
sudo apt-get install libxslt-dev libxml2-dev
bundle install
rails generate rspec:install

答案 9 :(得分:0)

哈哈,重启spork和自动测试就行了。它确实需要一个不错的踢。我正在运行rspec-rails 2.6.1顺便说一句......

答案 10 :(得分:0)

“Kicked”Spork并为我解决了这个问题。