rails3 rspec问题

时间:2010-09-12 15:59:43

标签: rspec bundler rspec2

我正在尝试使用rails3。我正在使用railstutorial 网站,以了解有关rails3的更多信息;本教程非常好(我对rails2的经验很少)。

我有一个rspec的问题,目前阻止了我的进展。我看到教程推荐使用rspec2.0.0.beta.18 gem;我改为使用

安装了rspec2.0.0.beta.20 gem
bundle install

但是我发现这个版本的rspec存在问题 我对于integration_test的rspec看起来像是:

describe "LayoutLinks" do
  it "should have a About page at '/about'" do  
    get '/about'
    response.should have_selector('h1', :content => "About Us")
  end 
end

失败如下:

Failures:
  1) LayoutLinks should have a About page at '/about'
     Failure/Error: Unable to find matching line from backtrace
     stack level too deep
     # /home/arun/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/forwardable.rb:185

(注意:那些查看Chapter 5的人不会有理解上下文的问题。)

如果我在Gemfile中将rspec版本更改为2.0.0.beta.18并运行rspec我会收到以下错误

arun@ubuntu-world:~/Project/Rails/rails3/sample_app$ rspec spec/
/home/arun/.rvm/gems/ruby-1.9.2-p0/gems/bundler-1.0.0/lib/bundler/runtime.rb:27:in `block in setup': You have already activated rspec-core 2.0.0.beta.20, but your Gemfile requires rspec-core 2.0.0.beta.18. Consider using bundle exec. (Gem::LoadError)
        from /home/arun/.rvm/gems/ruby-1.9.2-p0/gems/bundler-1.0.0/lib/bundler/spec_set.rb:12:in `block in each'
        from /home/arun/.rvm/gems/ruby-1.9.2-p0/gems/bundler-1.0.0/lib/bundler/spec_set.rb:12:in `each'
        from /home/arun/.rvm/gems/ruby-1.9.2-p0/gems/bundler-1.0.0/lib/bundler/spec_set.rb:12:in `each'
        from /home/arun/.rvm/gems/ruby-1.9.2-p0/gems/bundler-1.0.0/lib/bundler/runtime.rb:17:in `setup'
        from /home/arun/.rvm/gems/ruby-1.9.2-p0/gems/bundler-1.0.0/lib/bundler.rb:100:in `setup'
        from /home/arun/Project/Rails/rails3/sample_app/config/boot.rb:8:in `<top (required)>'
        from <internal:lib/rubygems/custom_require>:29:in `require'
        from <internal:lib/rubygems/custom_require>:29:in `require'
        from /home/arun/Project/Rails/rails3/sample_app/config/application.rb:1:in `<top (required)>'
        from <internal:lib/rubygems/custom_require>:29:in `require'
        from <internal:lib/rubygems/custom_require>:29:in `require'
        from /home/arun/Project/Rails/rails3/sample_app/config/environment.rb:2:in `<top (required)>'
        from <internal:lib/rubygems/custom_require>:29:in `require'
        from <internal:lib/rubygems/custom_require>:29:in `require'
        from /home/arun/Project/Rails/rails3/sample_app/spec/spec_helper.rb:3:in `<top (required)>'
        from <internal:lib/rubygems/custom_require>:29:in `require'
        from <internal:lib/rubygems/custom_require>:29:in `require'
        from /home/arun/Project/Rails/rails3/sample_app/spec/requests/layout_links_spec.rb:1:in `<top (required)>'
        from /home/arun/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.0.0.beta.20/lib/rspec/core/configuration.rb:302:in `load'
        from /home/arun/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.0.0.beta.20/lib/rspec/core/configuration.rb:302:in `block in load_spec_files'
        from /home/arun/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.0.0.beta.20/lib/rspec/core/configuration.rb:302:in `map'
        from /home/arun/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.0.0.beta.20/lib/rspec/core/configuration.rb:302:in `load_spec_files'
        from /home/arun/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.0.0.beta.20/lib/rspec/core/command_line.rb:18:in `run'
        from /home/arun/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.0.0.beta.20/lib/rspec/core/runner.rb:46:in `run_in_process'
        from /home/arun/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.0.0.beta.20/lib/rspec/core/runner.rb:37:in `run'
        from /home/arun/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.0.0.beta.20/lib/rspec/core/runner.rb:10:in `block in autorun'

7 个答案:

答案 0 :(得分:2)

我遇到了同样的问题。在the end of Chapter 5,来自 /spec/requests/layout_links_spec.rb 的测试都失败并出现相同的错误(控制器测试工作正常):

Failure/Error: Unable to find matching line from backtrace 
stack level too deep
# C:/Ruby192/lib/ruby/1.9.1/forwardable.rb:185

/spec/requests/layout_links_spec.rb 文件进行一些故障排除后,似乎在此上下文中使用response是触发问题的原因。例如,如果文件如下所示,则没有错误:

require 'spec_helper'

describe "LayoutLinks" do

    it "should run tests properly from this file" do
        get '/'
    end
end

但是从教程中复制的文件是:

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
end

一旦调用response

,就会抛出错误

这是在Windows 7上(我试图在系统级而不是在Cygwin中设置Ruby,Git,Vim和其他开发工具。)

Rspeicher - 按照RailsTutorial.org的步骤, /config/routes.rb 文件如下所示:

SampleApp::Application.routes.draw do
 get "users/new"

 match '/signup', :to => 'users#new'

 match '/contact', :to => 'pages#contact'
 match '/about', :to => 'pages#about'
 match '/help', :to => 'pages#help'

 root :to => 'pages#home'
end
像Arun说的那样,这一切都在浏览器中按预期工作,因此问题似乎在rspec或ruby中。我想如果在Cygwin下安装ruby,这不是问题。我希望不必恢复到纯Cygwin环境,特别是因为我的webroot和项目文件已经在Cygwin的虚拟unix文件夹结构之外进行管理。

答案 1 :(得分:1)

Rspec在Hartl教程的11章中一直很好。但不知何故,我搞砸了我的gemfille,我开始遇到同样的问题,rspec“堆栈级别太深”,无法通过'捆绑安装'来解决。所以我疯狂地做了一个

windows> gem uninstall rspec rspec-rails rspec-expectations rspec-mock rspec-core

(所有版本)。 '宝石清单'显示它们确实全部消失了。然后我执行了

> bundle install
...
Using rspec-core (2.0.0.beta.22)
Using rspec-expectations (2.0.0.beta.22)
Using rspec-mocks (2.0.0.beta.22)
Using rspec (2.0.0.beta.22)
Using rspec-rails (2.0.0.beta.22)
...

我的Gemfile中有什么。但是,'gem list -d rspec'仍然显示-no-rspec或rspec-xxx gems安装和rspec-core问题......

> rails g rspec:install       *#for good measure*
...
> rspec -v
C:/Ruby192/lib/ruby/1.9.1/rubygems.rb:762:in `report_activate_error': Could not 
find RubyGem rspec-core (>= 0) (Gem::LoadError)

但'捆绑列表'表示已安装。那么我如何让rspec再次运作呢?


更新

我解决了这个问题,但首先我向 arun kumar 道歉,因为他在他的问题上露营并介入它和答案之间。我认为我的问题很相似,我的“答案”将在最后出现。

首先,我必须做一个

> gem install rspec -v 2.0.0.beta.22
> gem install rspec-rails -v 2.0.0.beta.22

而不是期望捆绑者这样做。捆绑在这里似乎不可靠。这让我再次调用了rspec,但我仍然不得不处理“堆栈级别太深”。通过将rspec和rspec-rails升级到2.0.1(来自David Chelimski:http://www.ruby-forum.com/topic/282245)来解决这个问题。再次更改我的Gemfile并调用bundle-install不起作用。我不得不卸载上面的所有五个rspec-xxx宝石并手动执行

> gem install rspec -v 2.0.1
> gem install rspec-rails -v 2.0.1

也许我不需要先卸载,但我做到了。海报很可能已经解决了他的问题,因为它很久以前,但这是我的解决方案,可能适用于其他人 - 回想一下我有Windows Vista64。

答案 2 :(得分:0)

每当您更改Gemfile时,都应通过重新运行bundle install来修复第二个错误。

至于第一个错误,您可以在routes.rb文件中发布您定义'/ about'路线的行吗?

答案 3 :(得分:0)

我再次运行“bundle install”,当我查看Gemfile和Gemfile.lock时,无论是使用2.0.0.beta.18还是2.0.0.beta.20,我都会找到相应的gems。

routes.rb映射/关于属于特定控制器的某些特定操作。当我在浏览器中访问页面时,我也得到了正确的页面;所以我认为这不是问题所在。 (不幸的是我现在没有代码,不能在这里粘贴)

以下是我无法理解的错误

arun@ubuntu-world:~/Project/Rails/rails3/sample_app$ rspec spec/
/home/arun/.rvm/gems/ruby-1.9.2-p0/gems/bundler-1.0.0/lib/bundler/runtime.rb:27:in `block in setup': You have already activated rspec-core 2.0.0.beta.20, but your Gemfile requires rspec-core 2.0.0.beta.18. Consider using bundle exec. (Gem::LoadError)

我知道 bundle exec 是什么,更不用说如何使用它了......

- 阿伦

答案 4 :(得分:0)

您需要使用gem uninstall删除其中一个宝石:

$ gem uninstall rspec-core -v 2.0.0.beta.20

您还可以使用gem list查看已安装的宝石版本。

答案 5 :(得分:0)

它绝对与Windows或您拥有的任何环境无关。我的Mac遇到了同样的问题。

我的routes.rb文件(来自同一个教程)看起来像这样:

SampleApp::Application.routes.draw do

  resources :users

  match '/signup', :to => 'users#new'
  match '/contact', :to => 'pages#contact'
  match '/about', :to => 'pages#about'
  match '/help', :to => 'pages#help'

end

答案 6 :(得分:0)

您也可以尝试运行

bundle exec rake db:test:prepare