2

当我运行 rspec 进行测试时,我得到了这个错误:

无法加载此类文件 -- mocha/object (LoadError) 问题出在以下行config.mock_with :mocha

这是我的 spec_helper.rb:

require 'rubygems'
require 'spork'

Spork.prefork do
  # Loading more in this block will cause your tests to run faster. However, 
  # if you change any configuration or code from libraries loaded here, you'll
  # need to restart spork for it take effect.
  ENV["RAILS_ENV"] ||= 'test'
  require File.expand_path("../../config/environment", __FILE__)
  require 'rspec/rails'
  require 'rspec/autorun'
  require 'capybara/rails'
  require 'capybara/rspec'
  #require 'spork/ext/ruby-debug'
  # 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 MultiMock::Adapter.for(:rspec, :mocha) #, :rr, :not_a_mock, ::NewMockFramework::RSpecAdapter)

    # 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 = true

    # If true, the base class of anonymous controllers will be inferred
    # automatically. This will be the default behavior in future versions of
    # rspec-rails.
    config.infer_base_class_for_anonymous_controllers = false


  end

end

Spork.each_run do
  include Devise::TestHelpers


  require 'factory_girl_rails'


  # reload all the models
  Dir["#{Rails.root}/app/models/**/*.rb"].each do |model|
    load model
  end

  # reload all factories
  FactoryGirl.factories.clear
  Dir.glob("#{::Rails.root}/spec/factories/*.rb").each do |file|
    load "#{file}"
  end

  # reload routes
  Hope::Application.reload_routes!

end


RSpec.configure do |config|
  config.include Devise::TestHelpers, :type => :controller
end
RSpec.configure do |config|
  config.extend ControllerMacros, :type => :controller
end

那么,我在这里缺少什么?

编辑

这是完整的错误:

04:28:30 - INFO - Starting Spork for RSpec
Using RSpec
Preloading Rails environment
Loading Spork.prefork block...

*** Mocha deprecation warning: Test::Unit or MiniTest must be loaded *before* Mocha.


*** Mocha deprecation warning: If you're integrating with a test library other than Test::Unit or MiniTest, you should use `require 'mocha/api'` instead of `require 'mocha'`.

DEPRECATION WARNING: You have Rails 2.3-style plugins in vendor/plugins! Support for these plugins will be removed in Rails 4.0. Move them out and bundle them in your Gemfile, or fold them in to your app as lib/myplugin/* and config/initializers/myplugin.rb. See the release notes for more on this: http://weblog.rubyonrails.org/2012/1/4/rails-3-2-0-rc2-has-been-released. (called from <top (required)> at /home/dcaclab/RubymineProjects/coursebuilder/config/environment.rb:5)

[DEVISE] Devise.use_salt_as_remember_token is deprecated and has no effect. Please remove it.

Rack::File headers parameter replaces cache_control after Rack 1.5.

*** Mocha deprecation warning: Change `require 'mocha'` to `require 'mocha/setup'`.


*** Mocha deprecation warning: `require 'mocha/standalone'` has been deprecated. Please use `require 'mocha/api' instead.

cannot load such file -- mocha/object (LoadError)
/home/dcaclab/.rvm/gems/ruby-1.9.3-p362@course_builder/gems/activesupport-3.2.11/lib/active_support/dependencies.rb:251:in `require'
/home/dcaclab/.rvm/gems/ruby-1.9.3-p362@course_builder/gems/activesupport-3.2.11/lib/active_support/dependencies.rb:251:in `block in require'
/home/dcaclab/.rvm/gems/ruby-1.9.3-p362@course_builder/gems/activesupport-3.2.11/lib/active_support/dependencies.rb:236:in `load_dependency'
/home/dcaclab/.rvm/gems/ruby-1.9.3-p362@course_builder/gems/activesupport-3.2.11/lib/active_support/dependencies.rb:251:in `require'
/home/dcaclab/.rvm/gems/ruby-1.9.3-p362@course_builder/gems/rspec-core-2.9.0/lib/rspec/core/mocking/with_mocha.rb:2:in `<top (required)>'
/home/dcaclab/.rvm/gems/ruby-1.9.3-p362@course_builder/gems/activesupport-3.2.11/lib/active_support/dependencies.rb:251:in `require'
/home/dcaclab/.rvm/gems/ruby-1.9.3-p362@course_builder/gems/activesupport-3.2.11/lib/active_support/dependencies.rb:251:in `block in require'
/home/dcaclab/.rvm/gems/ruby-1.9.3-p362@course_builder/gems/activesupport-3.2.11/lib/active_support/dependencies.rb:236:in `load_dependency'
/home/dcaclab/.rvm/gems/ruby-1.9.3-p362@course_builder/gems/activesupport-3.2.11/lib/active_support/dependencies.rb:251:in `require'
/home/dcaclab/.rvm/gems/ruby-1.9.3-p362@course_builder/gems/rspec-core-2.9.0/lib/rspec/core/configuration.rb:315:in `mock_with'
/home/dcaclab/RubymineProjects/coursebuilder/spec/spec_helper.rb:26:in `block (2 levels) in <top (required)>'
/home/dcaclab/.rvm/gems/ruby-1.9.3-p362@course_builder/gems/rspec-core-2.9.0/lib/rspec/core.rb:94:in `configure'
/home/dcaclab/RubymineProjects/coursebuilder/spec/spec_helper.rb:21:in `block in <top (required)>'
/home/dcaclab/.rvm/gems/ruby-1.9.3-p362@course_builder/gems/spork-0.9.2/lib/spork.rb:24:in `prefork'
/home/dcaclab/RubymineProjects/coursebuilder/spec/spec_helper.rb:6:in `<top (required)>'
/home/dcaclab/.rvm/gems/ruby-1.9.3-p362@course_builder/gems/activesupport-3.2.11/lib/active_support/dependencies.rb:245:in `load'
/home/dcaclab/.rvm/gems/ruby-1.9.3-p362@course_builder/gems/activesupport-3.2.11/lib/active_support/dependencies.rb:245:in `block in load'
/home/dcaclab/.rvm/gems/ruby-1.9.3-p362@course_builder/gems/activesupport-3.2.11/lib/active_support/dependencies.rb:236:in `load_dependency'
/home/dcaclab/.rvm/gems/ruby-1.9.3-p362@course_builder/gems/activesupport-3.2.11/lib/active_support/dependencies.rb:245:in `load'
/home/dcaclab/.rvm/gems/ruby-1.9.3-p362@course_builder/gems/spork-0.9.2/lib/spork/test_framework.rb:138:in `block (2 levels) in preload'
/home/dcaclab/.rvm/gems/ruby-1.9.3-p362@course_builder/gems/spork-0.9.2/lib/spork/app_framework/rails.rb:8:in `preload'
/home/dcaclab/.rvm/gems/ruby-1.9.3-p362@course_builder/gems/spork-0.9.2/lib/spork/test_framework.rb:134:in `block in preload'
/home/dcaclab/.rvm/gems/ruby-1.9.3-p362@course_builder/gems/spork-0.9.2/lib/spork.rb:62:in `exec_prefork'
/home/dcaclab/.rvm/gems/ruby-1.9.3-p362@course_builder/gems/spork-0.9.2/lib/spork/test_framework.rb:120:in `preload'
/home/dcaclab/.rvm/gems/ruby-1.9.3-p362@course_builder/gems/spork-0.9.2/lib/spork/run_strategy/forking.rb:25:in `preload'
/home/dcaclab/.rvm/gems/ruby-1.9.3-p362@course_builder/gems/spork-0.9.2/lib/spork/runner.rb:74:in `run'
/home/dcaclab/.rvm/gems/ruby-1.9.3-p362@course_builder/gems/spork-0.9.2/lib/spork/runner.rb:10:in `run'
/home/dcaclab/.rvm/gems/ruby-1.9.3-p362@course_builder/gems/spork-0.9.2/bin/spork:10:in `<top (required)>'
/home/dcaclab/.rvm/gems/ruby-1.9.3-p362@course_builder/bin/spork:19:in `load'
/home/dcaclab/.rvm/gems/ruby-1.9.3-p362@course_builder/bin/spork:19:in `<main>'
/home/dcaclab/.rvm/gems/ruby-1.9.3-p362@course_builder/bin/ruby_noexec_wrapper:14:in `eval'
/home/dcaclab/.rvm/gems/ruby-1.9.3-p362@course_builder/bin/ruby_noexec_wrapper:14:in `<main>'

编辑 2

这是我的 Gemfile

source 'http://rubygems.org'

gem 'rails' , '3.2.11'

gem "heroku"

gem 'thin'

gem 'devise'#, '1.5.2'

gem 'will_paginate', '~> 3.0.2'

#gem 'nested_form', :git => 'git://github.com/fxposter/nested_form.git'

gem "simple_form"

gem "cancan"

gem 'kaminari'

gem 'foreigner'

gem "cocoon"

gem 'client_side_validations'

# Gems used only for assets and not required
# in production environments by default.
group :assets do
  gem 'sass-rails'#, "  ~> 3.1.0"
  gem 'coffee-rails'#, "~> 3.1.0"
  gem 'uglifier'
end

gem "jquery-rails"

gem "validate_url", "~> 0.2.0"



gem 'pg', :group => [:production]



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

# Deploy with Capistrano
# gem 'capistrano'

# To use debugger
# gem 'ruby-debug19', :require => 'ruby-debug'
group :test, :development do
  gem 'sqlite3'

  gem 'turn', :require => false
  gem "letter_opener"

  gem 'meta_request', '0.2.0'

  gem "rspec-rails", "2.9.0"

  gem 'guard-rspec'
  gem "spork"
  gem 'guard-spork'

  gem 'rb-inotify', '~> 0.8.8'

  gem "factory_girl_rails"

  gem "mocha", :require=> false
  gem 'capybara' #, :git => 'git://github.com/jnicklas/capybara.git'

  gem 'launchy'
  gem "capybara-webkit"

  gem "nifty-generators"

  gem 'sextant'

  gem 'haml-rails'
  gem 'hpricot'
  gem 'ruby_parser'
  #gem 'rspec-multi-mock', :git => 'git://github.com/endeepak/rspec-multi-mock.git'
end

group :test do
  #gem 'ruby-debug19', :require => 'ruby-debug'

end

gem 'execjs'

gem 'therubyracer'

这是我的 Gemfile.lock

GEM
  remote: http://rubygems.org/
  specs:
    actionmailer (3.2.11)
      actionpack (= 3.2.11)
      mail (~> 2.4.4)
    actionpack (3.2.11)
      activemodel (= 3.2.11)
      activesupport (= 3.2.11)
      builder (~> 3.0.0)
      erubis (~> 2.7.0)
      journey (~> 1.0.4)
      rack (~> 1.4.0)
      rack-cache (~> 1.2)
      rack-test (~> 0.6.1)
      sprockets (~> 2.2.1)
    activemodel (3.2.11)
      activesupport (= 3.2.11)
      builder (~> 3.0.0)
    activerecord (3.2.11)
      activemodel (= 3.2.11)
      activesupport (= 3.2.11)
      arel (~> 3.0.2)
      tzinfo (~> 0.3.29)
    activeresource (3.2.11)
      activemodel (= 3.2.11)
      activesupport (= 3.2.11)
    activesupport (3.2.11)
      i18n (~> 0.6)
      multi_json (~> 1.0)
    addressable (2.3.2)
    ansi (1.4.3)
    arel (3.0.2)
    bcrypt-ruby (3.0.1)
    builder (3.0.4)
    cancan (1.6.8)
    capybara (2.0.2)
      mime-types (>= 1.16)
      nokogiri (>= 1.3.3)
      rack (>= 1.0.0)
      rack-test (>= 0.5.4)
      selenium-webdriver (~> 2.0)
      xpath (~> 1.0.0)
    capybara-webkit (0.14.1)
      capybara (~> 2.0, >= 2.0.2)
      json
    childprocess (0.3.7)
      ffi (~> 1.0, >= 1.0.6)
    client_side_validations (3.2.1)
    cocoon (1.1.2)
    coderay (1.0.8)
    coffee-rails (3.2.2)
      coffee-script (>= 2.2.0)
      railties (~> 3.2.0)
    coffee-script (2.2.0)
      coffee-script-source
      execjs
    coffee-script-source (1.4.0)
    daemons (1.1.9)
    devise (2.2.3)
      bcrypt-ruby (~> 3.0)
      orm_adapter (~> 0.1)
      railties (~> 3.1)
      warden (~> 1.2.1)
    diff-lcs (1.1.3)
    erubis (2.7.0)
    eventmachine (1.0.0)
    excon (0.16.10)
    execjs (1.4.0)
      multi_json (~> 1.0)
    factory_girl (4.2.0)
      activesupport (>= 3.0.0)
    factory_girl_rails (4.2.0)
      factory_girl (~> 4.2.0)
      railties (>= 3.0.0)
    ffi (1.3.1)
    foreigner (1.3.0)
      activerecord (>= 3.0.0)
    guard (1.6.2)
      listen (>= 0.6.0)
      lumberjack (>= 1.0.2)
      pry (>= 0.9.10)
      terminal-table (>= 1.4.3)
      thor (>= 0.14.6)
    guard-rspec (1.2.1)
      guard (>= 1.1)
    guard-spork (1.4.1)
      childprocess (>= 0.2.3)
      guard (>= 1.1)
      spork (>= 0.8.4)
    haml (3.1.7)
    haml-rails (0.3.5)
      actionpack (>= 3.1, < 4.1)
      activesupport (>= 3.1, < 4.1)
      haml (~> 3.1)
      railties (>= 3.1, < 4.1)
    heroku (2.34.0)
      heroku-api (~> 0.3.7)
      launchy (>= 0.3.2)
      netrc (~> 0.7.7)
      rest-client (~> 1.6.1)
      rubyzip
    heroku-api (0.3.7)
      excon (~> 0.16.10)
    hike (1.2.1)
    hpricot (0.8.6)
    i18n (0.6.1)
    journey (1.0.4)
    jquery-rails (2.2.0)
      railties (>= 3.0, < 5.0)
      thor (>= 0.14, < 2.0)
    json (1.7.6)
    kaminari (0.14.1)
      actionpack (>= 3.0.0)
      activesupport (>= 3.0.0)
    launchy (2.1.2)
      addressable (~> 2.3)
    letter_opener (1.0.0)
      launchy (>= 2.0.4)
    libv8 (3.11.8.13)
    listen (0.7.2)
    lumberjack (1.0.2)
    mail (2.4.4)
      i18n (>= 0.4.0)
      mime-types (~> 1.16)
      treetop (~> 1.4.8)
    meta_request (0.2.0)
      rack-contrib
      rails
    metaclass (0.0.1)
    method_source (0.8.1)
    mime-types (1.19)
    mocha (0.13.2)
      metaclass (~> 0.0.1)
    multi_json (1.5.0)
    netrc (0.7.7)
    nifty-generators (0.4.6)
    nokogiri (1.5.6)
    orm_adapter (0.4.0)
    pg (0.14.1)
    polyglot (0.3.3)
    pry (0.9.11.4)
      coderay (~> 1.0.5)
      method_source (~> 0.8)
      slop (~> 3.4)
    rack (1.4.4)
    rack-cache (1.2)
      rack (>= 0.4)
    rack-contrib (1.1.0)
      rack (>= 0.9.1)
    rack-ssl (1.3.3)
      rack
    rack-test (0.6.2)
      rack (>= 1.0)
    rails (3.2.11)
      actionmailer (= 3.2.11)
      actionpack (= 3.2.11)
      activerecord (= 3.2.11)
      activeresource (= 3.2.11)
      activesupport (= 3.2.11)
      bundler (~> 1.0)
      railties (= 3.2.11)
    railties (3.2.11)
      actionpack (= 3.2.11)
      activesupport (= 3.2.11)
      rack-ssl (~> 1.3.2)
      rake (>= 0.8.7)
      rdoc (~> 3.4)
      thor (>= 0.14.6, < 2.0)
    rake (10.0.3)
    rb-inotify (0.8.8)
      ffi (>= 0.5.0)
    rdoc (3.12)
      json (~> 1.4)
    ref (1.0.2)
    rest-client (1.6.7)
      mime-types (>= 1.16)
    rspec (2.9.0)
      rspec-core (~> 2.9.0)
      rspec-expectations (~> 2.9.0)
      rspec-mocks (~> 2.9.0)
    rspec-core (2.9.0)
    rspec-expectations (2.9.1)
      diff-lcs (~> 1.1.3)
    rspec-mocks (2.9.0)
    rspec-rails (2.9.0)
      actionpack (>= 3.0)
      activesupport (>= 3.0)
      railties (>= 3.0)
      rspec (~> 2.9.0)
    ruby_parser (3.1.1)
      sexp_processor (~> 4.1)
    rubyzip (0.9.9)
    sass (3.2.5)
    sass-rails (3.2.6)
      railties (~> 3.2.0)
      sass (>= 3.1.10)
      tilt (~> 1.3)
    selenium-webdriver (2.29.0)
      childprocess (>= 0.2.5)
      multi_json (~> 1.0)
      rubyzip
      websocket (~> 1.0.4)
    sexp_processor (4.1.4)
    sextant (0.2.3)
      activesupport (>= 3.2)
      rails (>= 3.2)
    simple_form (2.0.4)
      actionpack (~> 3.0)
      activemodel (~> 3.0)
    slop (3.4.3)
    spork (0.9.2)
    sprockets (2.2.2)
      hike (~> 1.2)
      multi_json (~> 1.0)
      rack (~> 1.0)
      tilt (~> 1.1, != 1.3.0)
    sqlite3 (1.3.7)
    terminal-table (1.4.5)
    therubyracer (0.11.3)
      libv8 (~> 3.11.8.12)
      ref
    thin (1.5.0)
      daemons (>= 1.0.9)
      eventmachine (>= 0.12.6)
      rack (>= 1.0.0)
    thor (0.17.0)
    tilt (1.3.3)
    treetop (1.4.12)
      polyglot
      polyglot (>= 0.3.1)
    turn (0.9.6)
      ansi
    tzinfo (0.3.35)
    uglifier (1.3.0)
      execjs (>= 0.3.0)
      multi_json (~> 1.0, >= 1.0.2)
    validate_url (0.2.0)
      activemodel (>= 3.0.0)
    warden (1.2.1)
      rack (>= 1.0)
    websocket (1.0.7)
    will_paginate (3.0.4)
    xpath (1.0.0)
      nokogiri (~> 1.3)

PLATFORMS
  ruby

DEPENDENCIES
  cancan
  capybara
  capybara-webkit
  client_side_validations
  cocoon
  coffee-rails
  devise
  execjs
  factory_girl_rails
  foreigner
  guard-rspec
  guard-spork
  haml
  haml-rails
  heroku
  hpricot
  jquery-rails
  kaminari
  launchy
  letter_opener
  meta_request (= 0.2.0)
  mocha
  nifty-generators
  pg
  rails (= 3.2.11)
  rb-inotify (~> 0.8.8)
  rspec-rails (= 2.9.0)
  ruby_parser
  sass-rails
  sextant
  simple_form
  spork
  sqlite3
  therubyracer
  thin
  turn
  uglifier
  validate_url (~> 0.2.0)
  will_paginate (~> 3.0.2)
4

2 回答 2

4

正如这里所说:https://github.com/freerange/mocha#bundler,您需要确保 Mocha 不是自动需要的,然后明确要求它spec_helper.rb

所以你Gemfile应该有这样的东西:

gem "mocha", :require => false

然后你spec_helper.rb应该有以下(在你的其他要求没问题之后):

require "mocha/setup"
于 2013-01-30T00:04:57.950 回答
3

关于你提出的 mocha问题,我已经更全面地回答了这个问题,但基本上我认为你需要升级 rspec(-rails) 或降级 mocha。您使用的两个版本互不兼容。

于 2013-01-30T11:40:16.270 回答