找不到文件--rspec/matchers - rspec-rails,shoulda,cucumber,factory girl,Rails2.3.10。

8
我接手了一个老旧的Rails2.3应用程序。它非常复杂,令人震惊的是,没有任何测试。我习惯于使用rspec和cucumber,因此我想开始编写规范和功能定义,以便最终(很长时间之后)升级到Rails 3。如果我知道会出现什么问题,升级就更容易了。
无论如何,我发现在互联网上很难找到关于如何在Rails 2.3环境中设置rspec、cucumber、factory girl和shoulda的资源。我的bundle如下:
source "http://rubygems.org"

# Production gems
gem "rails",    "2.3.10"
gem "nokogiri", "1.4.4"
gem "mysql",    "~> 2.8.1"

group :development do
  # bundler requires these gems in development
  gem 'rspec',          '1.3.2'
  gem 'rspec-core',     '2.5.2'
  gem 'rspec-rails',    '1.3.4'
end

group :test do
  # bundler requires these gems while running tests
  gem 'cucumber-rails', '0.4.1'
  gem 'factory_girl'
  gem 'shoulda',        '2.11.3'
  gem 'shoulda-matchers'
end

当我要运行时
bundle exec rspec spec, 

我从shoulda得到了以下的堆栈跟踪:

/Users/sys/src/proj/rails/ruby/1.8/gems/shoulda-2.11.3/lib/shoulda/integrations/rspec2.rb:8: no such file to load -- rspec/matchers (MissingSourceFile)
    from /Users/sys/src/proj/vendor/rails/activesupport/lib/active_support/dependencies.rb:184:in `require'
    from /Users/sys/src/proj/vendor/rails/activesupport/lib/active_support/dependencies.rb:184:in `require'
    from /Users/sys/src/proj/rails/ruby/1.8/gems/shoulda-2.11.3/lib/shoulda.rb:4
    from /Library/Ruby/Gems/1.8/gems/bundler-1.0.14/lib/bundler/runtime.rb:68:in `require'
    from /Library/Ruby/Gems/1.8/gems/bundler-1.0.14/lib/bundler/runtime.rb:68:in `require'
    from /Library/Ruby/Gems/1.8/gems/bundler-1.0.14/lib/bundler/runtime.rb:66:in `each'
    from /Library/Ruby/Gems/1.8/gems/bundler-1.0.14/lib/bundler/runtime.rb:66:in `require'
    from /Library/Ruby/Gems/1.8/gems/bundler-1.0.14/lib/bundler/runtime.rb:55:in `each'
    from /Library/Ruby/Gems/1.8/gems/bundler-1.0.14/lib/bundler/runtime.rb:55:in `require'
    from /Library/Ruby/Gems/1.8/gems/bundler-1.0.14/lib/bundler.rb:120:in `require'
    from /Users/sys/src/proj/config/boot.rb:119:in `load_gems'
    from /Users/sys/src/proj/config/../vendor/rails/railties/lib/initializer.rb:164:in `process'
    from /Users/sys/src/proj/config/../vendor/rails/railties/lib/initializer.rb:113:in `send'
    from /Users/sys/src/proj/config/../vendor/rails/railties/lib/initializer.rb:113:in `run'
    from /Users/sys/src/proj/config/environment.rb:12
    from /Users/sys/src/proj/spec/spec_helper.rb:4:in `require'
    from /Users/sys/src/proj/spec/spec_helper.rb:4
    from /Users/sys/src/proj/spec/models/announcement_spec.rb:1:in `require'
    from /Users/sys/src/proj/spec/models/announcement_spec.rb:1
    from /Users/sys/src/proj/rails/ruby/1.8/gems/rspec-core-2.5.2/lib/rspec/core/configuration.rb:386:in `load'
    from /Users/sys/src/proj/rails/ruby/1.8/gems/rspec-core-2.5.2/lib/rspec/core/configuration.rb:386:in `load_spec_files'
    from /Users/sys/src/proj/rails/ruby/1.8/gems/rspec-core-2.5.2/lib/rspec/core/configuration.rb:386:in `map'
    from /Users/sys/src/proj/rails/ruby/1.8/gems/rspec-core-2.5.2/lib/rspec/core/configuration.rb:386:in `load_spec_files'
    from /Users/sys/src/proj/rails/ruby/1.8/gems/rspec-core-2.5.2/lib/rspec/core/command_line.rb:18:in `run'
    from /Users/sys/src/proj/rails/ruby/1.8/gems/rspec-core-2.5.2/lib/rspec/core/runner.rb:55:in `run_in_process'
    from /Users/sys/src/proj/rails/ruby/1.8/gems/rspec-core-2.5.2/lib/rspec/core/runner.rb:46:in `run'
    from /Users/sys/src/proj/rails/ruby/1.8/gems/rspec-core-2.5.2/lib/rspec/core/runner.rb:10:in `autorun'
    from /Users/sys/src/proj/rails/ruby/1.8/bin/rspec:19

在谷歌上搜索这个错误,结果非常少...我不确定我做错了什么?

删除rspec-core后,执行以下命令:

bundle exec rspec spec/models/announcement_spec.rb /Users/sys/.rvm/gems/ree-1.8.7-2011.03@proj/gems/bundler-1.0.14/lib/bundler/rubygems_integration.rb:143:in `gem': rspec-core不是bundle的一部分。将其添加到Gemfile中。(Gem::LoadError)

opt/local/bin/rspec的内容。

cat /opt/local/bin/rspec 
#!/usr/bin/env ruby
#
# This file was generated by RubyGems.
#
# The application 'rspec-core' is installed as part of a gem, and
# this file is here to facilitate running it.
#

require 'rubygems'

version = ">= 0"

if ARGV.first =~ /^_(.*)_$/ and Gem::Version.correct? $1 then
  version = $1
  ARGV.shift
end

gem 'rspec-core', version
load Gem.bin_path('rspec-core', 'rspec', version)

我想知道这是否会与我已安装的其他rspec冲突(我已经安装了一个Rails3应用程序,也使用了bundler/rvm)......不确定为什么rspec要在opt/local/bin中运行而不是bundle中运行。

3个回答

11

我也遇到了同样的问题。经过查找所有二进制rspec命令(几个位置),我才晚晚意识到RSPEC 1.3.2分支将其二进制文件命名为“spec”,而不是“rspec”。因此,正确的命令始终是"bundle exec spec spec/models/..."。 如果使用"rspec",bundler无法在当前的rvm/gemset中找到它,于是去整个路径中搜索,最终找到了属于rspec2.x安装的/opt/local/bin副本。

可能会有一个别名可以让rspec 1.3.2也响应“rspec”命令,但对我来说并非如此。使用“spec”可以100%可靠地避免使用rspec-core和其他2.x模块的调用。

感谢提出这个问题并做出以上所有研究的Charles。


1

看起来rspec-core是问题所在,它源自一个只支持Rails 3的rspec版本。尝试卸载它,并从Gemfile中删除它。

为了比较,这里有一个在Rails 2.3上运行良好的Gemfile部分:

group :development, :local, :test do                                             
  gem "rspec",              "= 1.3.1"                                            
  gem "rspec-rails",        "= 1.3.3"                                            
  gem "factory_girl",       "= 1.3.3"                                            
  gem "webrat",             "= 0.7.3"                                            
  gem "shoulda",            "= 2.11.3"                                           
end        

如果我删除rspec-core,会得到以下错误:/Library/Ruby/Gems/1.8/gems/bundler-1.0.14/lib/bundler/rubygems_integration.rb:143:in `gem': rspec-core不是bundle的一部分。将其添加到Gemfile中。(Gem::LoadError) from /Users/sys/src/proj/rails/ruby/1.8/bin/rspec:18 - Matt Rogish
我建议你卸载所有的rspec宝石,只在Gemfile中添加上述两个宝石,然后运行bundle install。 - Mori
仍然存在问题。但是,现在它抱怨文件位于 /opt/local/bin/rspec:18 中... 那个脚本的文件内容是什么? - Matt Rogish
1
为确保您运行正确的可执行文件,请使用bundle exec,例如 bundle exec rake spec - Mori
1
你知道吗,如果有一个网站可以列出不同 gem 环境下给定版本的 Rails 的所有依赖关系,那真的会非常有帮助。有很多类似这样的问题,但很少有明确的答案(很多答案是部分的,错过了各种依赖项)。不幸的是,Bundler 也不能总是为你选择正确的版本! - rholmes

1

在 Rails 3.1 上,bundle update rspec-rails 解决了我的这个问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接