2014-03-01 67 views
0

我正在開發一個簡單的使用MongoDb作爲數據庫(使用Mongoid)的rails應用程序。我一直在應用程序中做我的第一個模型,所以我想使用rspec進行測試。搜索後,我發現了rspec-rails和mongoid-rspec寶石,我遵循了他們的安裝說明。但是,當我執行:軌摹rspec的:安裝,我得到了以下錯誤:執行導軌的錯誤

home/jesus/.rvm/gems/ruby-2.0.0-p353/gems/mongoid-3.1.6/lib/mongoid/config/validators/session.rb:99:in `no_database_or_uri?': undefined method `has_key?' for nil:NilClass (NoMethodError) 
    from /home/jesus/.rvm/gems/ruby-2.0.0-p353/gems/mongoid-3.1.6/lib/mongoid/config/validators/session.rb:45:in `validate_session_database' 
    from /home/jesus/.rvm/gems/ruby-2.0.0-p353/gems/mongoid-3.1.6/lib/mongoid/config/validators/session.rb:25:in `block in validate' 
    from /home/jesus/.rvm/gems/ruby-2.0.0-p353/gems/mongoid-3.1.6/lib/mongoid/config/validators/session.rb:24:in `each_pair' 
    from /home/jesus/.rvm/gems/ruby-2.0.0-p353/gems/mongoid-3.1.6/lib/mongoid/config/validators/session.rb:24:in `validate' 
    from /home/jesus/.rvm/gems/ruby-2.0.0-p353/gems/mongoid-3.1.6/lib/mongoid/config.rb:239:in `sessions=' 
    from /home/jesus/.rvm/gems/ruby-2.0.0-p353/gems/mongoid-3.1.6/lib/mongoid/config.rb:136:in `load_configuration' 
    from /home/jesus/.rvm/gems/ruby-2.0.0-p353/gems/mongoid-3.1.6/lib/mongoid/config.rb:93:in `load!' 
    from /home/jesus/.rvm/gems/ruby-2.0.0-p353/gems/mongoid-3.1.6/lib/mongoid.rb:163:in `load!' 
    from /home/jesus/.rvm/gems/ruby-2.0.0-p353/gems/mongoid-3.1.6/lib/mongoid/railtie.rb:67:in `block in <class:Railtie>' 
    from /home/jesus/.rvm/gems/ruby-2.0.0-p353/gems/railties-3.2.15/lib/rails/initializable.rb:30:in `instance_exec' 
    from /home/jesus/.rvm/gems/ruby-2.0.0-p353/gems/railties-3.2.15/lib/rails/initializable.rb:30:in `run' 
    from /home/jesus/.rvm/gems/ruby-2.0.0-p353/gems/railties-3.2.15/lib/rails/initializable.rb:55:in `block in run_initializers' 
    from /home/jesus/.rvm/gems/ruby-2.0.0-p353/gems/railties-3.2.15/lib/rails/initializable.rb:54:in `each' 
    from /home/jesus/.rvm/gems/ruby-2.0.0-p353/gems/railties-3.2.15/lib/rails/initializable.rb:54:in `run_initializers' 
    from /home/jesus/.rvm/gems/ruby-2.0.0-p353/gems/railties-3.2.15/lib/rails/application.rb:136:in `initialize!' 
    from /home/jesus/.rvm/gems/ruby-2.0.0-p353/gems/railties-3.2.15/lib/rails/railtie/configurable.rb:30:in `method_missing' 
    from /home/jesus/rails_projects/myapp/config/environment.rb:5:in `<top (required)>' 
    from /home/jesus/.rvm/gems/ruby-2.0.0-p353/gems/activesupport-3.2.15/lib/active_support/dependencies.rb:251:in `require' 
    from /home/jesus/.rvm/gems/ruby-2.0.0-p353/gems/activesupport-3.2.15/lib/active_support/dependencies.rb:251:in `block in require' 
    from /home/jesus/.rvm/gems/ruby-2.0.0-p353/gems/activesupport-3.2.15/lib/active_support/dependencies.rb:236:in `load_dependency' 
    from /home/jesus/.rvm/gems/ruby-2.0.0-p353/gems/activesupport-3.2.15/lib/active_support/dependencies.rb:251:in `require' 
    from /home/jesus/.rvm/gems/ruby-2.0.0-p353/gems/railties-3.2.15/lib/rails/application.rb:103:in `require_environment!' 
    from /home/jesus/.rvm/gems/ruby-2.0.0-p353/gems/railties-3.2.15/lib/rails/commands.rb:25:in `<top (required)>' 
    from script/rails:6:in `require' 
    from script/rails:6:in `<main>' 

我一直在上網衝浪,但我無法找到任何東西。這是我一直在使用的主配置文件:

配置/ mongoid.yml

development: 
    sessions: 
    default: 
     database: myapp_development 
     hosts: 
     - localhost:27017 
     options: 
    options: 
test: 
    sessions: 
    default: 
     database: myapp_test 
     hosts: 
     - localhost:27017 
     options: 
     consistency: :strong 
     max_retries: 1 
     retry_interval: 0 

配置/初始化/ mongo.rb

MongoMapper.connection = Mongo::Connection.new('localhost', 27017) 
MongoMapper.database = "myapp_#{Rails.env}" 

if defined?(PhusionPassenger) 
    PhusionPassenger.on_event(:starting_worker_process) do |forked| 
    MongoMapper.connection.connect if forked 
    end 
end 

的Gemfile

source 'https://rubygems.org' 

gem 'rails', '3.2.15' 

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

# Gems used only for assets and not required 
# in production environments by default. 
group :assets do 
    gem 'sass-rails', '~> 3.2.3' 
    gem 'jquery-rails', '3.0.4' 
    gem 'jquery-ui-rails', '~>4.1.0' 
    gem 'coffee-rails', '~> 3.2.1' 

    # See https://github.com/sstephenson/execjs#readme for more supported runtimes 
    gem 'therubyracer' 

    gem 'uglifier', '>= 1.0.3' 
end 

gem 'mongo', '1.9.2' 
gem 'bson_ext', '1.9.2' 

gem 'fancybox-rails', "~> 0.2.1" 

gem 'mongo_mapper' 
gem 'mongoid', '~> 3.1.6' 

gem 'haml', "~> 4.0.4" 

gem 'i18n', '~> 0.6.9' 
gem 'i18n-js', '2.1.2' 
gem 'mongo-i18n', '~> 0.0.3' 

gem 'underscore-rails', '~> 1.5.2' 

gem 'devise', '~> 3.2.2' 

gem 'less-rails', '~> 2.4.2' 
gem 'twitter-bootstrap-rails', '~> 2.2.8' 

gem 'mongoid-paperclip', '~> 0.0.8' 

group :test, :development do 
    gem "rspec-rails", "~> 2.14.1" 
    gem "mongoid-rspec", "~> 1.11.0" 
end 

group :test do 
    gem 'database_cleaner' 
end 

當我嘗試初始化服務器(rails s)或使用控制檯(rails c)時,我獲得相同的錯誤。我花了很多時間試圖找到原因,但我找不到任何東西。

在此先感謝。

PD:讓我知道,如果你需要更多的配置文件,模型,spec_helper ...

回答

1

看來你沒有的MongoDB在端口上運行27017

這樣做是爲了啓動它:

$ mongod 
+0

你是對的!但不僅如此。它正在工作,但它沒有使用配置文件(/etc/mongodb.conf)。 – ReyLitch

+0

對不起,最後的答案。現在我可以執行測試並且運行良好。但是,當我嘗試執行rails s或rails c時,我會得到相同的錯誤。 – ReyLitch

+0

當然你的'database.yml'的開發設置是錯誤的。 – Agis