【问题标题】:Rspec2, Rails3, can't run rspec specRspec2,Rails3,无法运行 rspec 规范
【发布时间】:2011-08-20 10:10:08
【问题描述】:

我刚刚在我的 Rails3 (3.0.3) 项目中安装了 Rspec2。当我从 rails root 运行 rspec spec 时,我得到:

No examples were matched. Perhaps {:if=>#<Proc:0x00000100933388@/Users/ivan/.rvm/gems/ruby-1.9.2-p180/gems/rspec-core-2.5.1/lib/rspec/core/configuration.rb:50 (lambda)>, :unless=>#<Proc:0x00000100933360@/Users/ivan/.rvm/gems/ruby-1.9.2-p180/gems/rspec-core-2.5.1/lib/rspec/core/configuration.rb:51 (lambda)>} is excluding everything?


Finished in 0.00004 seconds
0 examples, 0 failures

我可以直接运行 rspec name_of_spec_test.rb,这似乎有效,但不是通用的“rspec spec”命令。另外,如果我将 require 'spec_helper.rb' 添加到我的 name_of_spec_test.rb 文件的顶部,我会得到:

/Users/ivan/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/net/http.rb:644:in `initialize': Connection refused - connect(2) (Errno::ECONNREFUSED)
    from /Users/ivan/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/net/http.rb:644:in `open'
    from /Users/ivan/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/net/http.rb:644:in `block in connect'
    from /Users/ivan/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/timeout.rb:44:in `timeout'
    from /Users/ivan/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/timeout.rb:87:in `timeout'
    from /Users/ivan/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/net/http.rb:644:in `connect'
    from /Users/ivan/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/net/http.rb:637:in `do_start'
    from /Users/ivan/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/net/http.rb:626:in `start'
    from /Users/ivan/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/net/http.rb:1168:in `request'
    from /Users/ivan/.rvm/gems/ruby-1.9.2-p180/gems/rsolr-1.0.0/lib/rsolr/connection.rb:15:in `execute'
    from /Users/ivan/.rvm/gems/ruby-1.9.2-p180/gems/rsolr-1.0.0/lib/rsolr/client.rb:151:in `execute'
    from /Users/ivan/.rvm/gems/ruby-1.9.2-p180/gems/rsolr-1.0.0/lib/rsolr/client.rb:146:in `send_and_receive'
    from (eval):2:in `post'
    from /Users/ivan/.rvm/gems/ruby-1.9.2-p180/gems/rsolr-1.0.0/lib/rsolr/client.rb:55:in `update'
    from /Users/ivan/.rvm/gems/ruby-1.9.2-p180/gems/rsolr-1.0.0/lib/rsolr/client.rb:75:in `add'
    from /Users/ivan/Documents/Projects/catalog_service.git/app/models/search/cake_to_solr.rb:39:in `<class:Playlist>'
    from /Users/ivan/Documents/Projects/catalog_service.git/app/models/search/cake_to_solr.rb:11:in `<top (required)>'
    from /Users/ivan/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:239:in `require'
    from /Users/ivan/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:239:in `block in require'
    from /Users/ivan/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:227:in `load_dependency'
    from /Users/ivan/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:239:in `require'
    from /Users/ivan/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:346:in `require_or_load'
    from /Users/ivan/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:300:in `depend_on'
    from /Users/ivan/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:216:in `require_dependency'
    from /Users/ivan/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.7/lib/rails/engine.rb:138:in `block (2 levels) in eager_load!'
    from /Users/ivan/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.7/lib/rails/engine.rb:137:in `each'
    from /Users/ivan/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.7/lib/rails/engine.rb:137:in `block in eager_load!'
    from /Users/ivan/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.7/lib/rails/engine.rb:135:in `each'
    from /Users/ivan/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.7/lib/rails/engine.rb:135:in `eager_load!'
    from /Users/ivan/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.7/lib/rails/application.rb:108:in `eager_load!'
    from /Users/ivan/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.7/lib/rails/application/finisher.rb:41:in `block in <module:Finisher>'
    from /Users/ivan/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.7/lib/rails/initializable.rb:25:in `instance_exec'
    from /Users/ivan/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.7/lib/rails/initializable.rb:25:in `run'
    from /Users/ivan/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.7/lib/rails/initializable.rb:50:in `block in run_initializers'
    from /Users/ivan/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.7/lib/rails/initializable.rb:49:in `each'
    from /Users/ivan/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.7/lib/rails/initializable.rb:49:in `run_initializers'
    from /Users/ivan/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.7/lib/rails/application.rb:134:in `initialize!'
    from /Users/ivan/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.7/lib/rails/application.rb:77:in `method_missing'
    from /Users/ivan/Documents/Projects/catalog_service.git/config/environment.rb:5:in `<top (required)>'
    from <internal:lib/rubygems/custom_require>:29:in `require'
    from <internal:lib/rubygems/custom_require>:29:in `require'
    from /Users/ivan/Documents/Projects/catalog_service.git/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 /Users/ivan/Documents/Projects/catalog_service.git/spec/models/search_test.rb:1:in `<top (required)>'
    from /Users/ivan/.rvm/gems/ruby-1.9.2-p180/gems/rspec-core-2.5.1/lib/rspec/core/configuration.rb:386:in `load'
    from /Users/ivan/.rvm/gems/ruby-1.9.2-p180/gems/rspec-core-2.5.1/lib/rspec/core/configuration.rb:386:in `block in load_spec_files'
    from /Users/ivan/.rvm/gems/ruby-1.9.2-p180/gems/rspec-core-2.5.1/lib/rspec/core/configuration.rb:386:in `map'
    from /Users/ivan/.rvm/gems/ruby-1.9.2-p180/gems/rspec-core-2.5.1/lib/rspec/core/configuration.rb:386:in `load_spec_files'
    from /Users/ivan/.rvm/gems/ruby-1.9.2-p180/gems/rspec-core-2.5.1/lib/rspec/core/command_line.rb:18:in `run'
    from /Users/ivan/.rvm/gems/ruby-1.9.2-p180/gems/rspec-core-2.5.1/lib/rspec/core/runner.rb:55:in `run_in_process'
    from /Users/ivan/.rvm/gems/ruby-1.9.2-p180/gems/rspec-core-2.5.1/lib/rspec/core/runner.rb:46:in `run'
    from /Users/ivan/.rvm/gems/ruby-1.9.2-p180/gems/rspec-core-2.5.1/lib/rspec/core/runner.rb:10:in `block in autorun'

我正在使用 RVM 来管理我的 rails 环境。

我不知道这是否相关,但是当我最初运行 script/rails generate rspec:install 时,它只安装了 .rspec,创建了 spec 目录和 spec/spec_helper.rb。

另外,我没有使用 Rails 迁移,因为我们有各种要连接的数据库。因此,当我创建模型并对其进行测试时,我会手动创建一个 spec/models 目录并将我的 *_test.rb rspec 文件放入其中。

任何关于我在这里遗漏的想法,将不胜感激!

这是我的 Gemfile、spec_helper.rb 文件和 test.rb 文件。

宝石文件:

source 'http://rubygems.org'

gem 'rails'
gem 'mysql2', '0.2.6'
gem 'legacy_data'
gem 'htmlentities'
gem 'httparty'
gem 'net-ssh'
gem 'net-sftp'
gem 'rsolr'
gem 'activerecord-import'
gem 'ar-extensions'


# Use unicorn as the web server
# gem 'unicorn'

# Deploy with Capistrano
# gem 'capistrano'

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

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

# Gems for RSpec
# Bundle gems for the local environment. Make sure to
# put test-only gems in this group so their generators
# and rake tasks are available in development mode:
group :development, :test do
   gem 'rspec-rails'
   gem 'webrat'
   gem 'rspec'
end
gem 'autotest'

spec_helper.rb:

# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'

# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}

RSpec.configure do |config|
  # == Mock Framework
  #
  # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
  #
  # config.mock_with :mocha
  # config.mock_with :flexmock
  # config.mock_with :rr
  config.mock_with :rspec

  # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
  # config.fixture_path = "#{::Rails.root}/spec/fixtures"

  # If you're not using ActiveRecord, or you'd prefer not to run each of your
  # examples within a transaction, remove the following line or assign false
  # instead of true.
  config.use_transactional_fixtures = false
end

test.rb:

CatalogService::Application.configure do
  # Settings specified here will take precedence over those in config/application.rb

  # The test environment is used exclusively to run your application's
  # test suite.  You never need to work with it otherwise.  Remember that
  # your test database is "scratch space" for the test suite and is wiped
  # and recreated between test runs.  Don't rely on the data there!
  config.cache_classes = true

  # Log error messages when you accidentally call methods on nil.
  config.whiny_nils = true

  # Show full error reports and disable caching
  config.consider_all_requests_local       = true
  config.action_controller.perform_caching = false

  # Raise exceptions instead of rendering exception templates
  config.action_dispatch.show_exceptions = false

  # Disable request forgery protection in test environment
  config.action_controller.allow_forgery_protection    = false

  # Tell Action Mailer not to deliver emails to the real world.
  # The :test delivery method accumulates sent emails in the
  # ActionMailer::Base.deliveries array.
  config.action_mailer.delivery_method = :test

  # Use SQL instead of Active Record's schema dumper when creating the test database.
  # This is necessary if your schema can't be completely dumped by the schema dumper,
  # like if you have constraints or database-specific column types
  # config.active_record.schema_format = :sql

  # Print deprecation notices to the stderr
  config.active_support.deprecation = :stderr

end

提前感谢您的任何想法!

【问题讨论】:

  • 与您使用的 *_test.rb 相比,rspec 通常不喜欢将规范文件命名为 *_spec.rb 吗?
  • 布赖恩 - 你是对的!感谢您指出了这一点。将文件重命名为 *_spec.rb 后,运行 rspec spec 会在 spec 文件夹中找到任何 _spec 文件。谢谢!
  • 很高兴这部分对你有用

标签: ruby-on-rails rspec2


【解决方案1】:

看起来你通过添加 spec_helper 是正确的,并且可能将 rsolr 放在一个组中 => [:development, :production],因为看起来测试环境正在加载 Solr 并且由于某种原因而失败?

【讨论】:

  • 嗨 Ryan - 谢谢你的想法......不幸的是,在这种情况下,我在测试期间实际上需要 rsolr,因为我正在测试的一部分是 Solr 的接口,并且需要 rsolr 库.虽然仍在追求这个想法......错误流表明 cake_to_solr.rb 脚本被称为我的模型之一,即使在这个测试中,我根本不使用那个模型......追求这个来看看为什么 rspec将尝试使用该模型...
【解决方案2】:

您可以检查您的默认模式。看起来您说您的文件被命名为“spec/models/XXXX_test.rb”,而 rspec 会查找默认结尾的“_spec.rb”。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-05
    • 1970-01-01
    • 1970-01-01
    • 2014-09-21
    • 2019-11-18
    相关资源
    最近更新 更多