当我在rails console
中尝试访问模型类时,引发了此错误:
Post.all.first
完整的错误说明:
Mongoid::Errors::NoClientConfig:
message:
No configuration could be found for a client named 'default'.
summary:
When attempting to create the new client, Mongoid could not find a client configuration for the name: 'default'. This is necessary in order to know the host, port, and options needed to connect.
resolution:
Double check your mongoid.yml to make sure under the clients key that a configuration exists for 'default'. If you have set the configuration programatically, ensure that 'default' exists in the configuration hash.
from /home/vagrant/.bundle/ruby/2.3.0/mongoid-f539cc6acd28/lib/mongoid/clients/factory.rb:26:in `create'
from /home/vagrant/.bundle/ruby/2.3.0/mongoid-f539cc6acd28/lib/mongoid/clients.rb:64:in `with_name'
from /home/vagrant/.bundle/ruby/2.3.0/mongoid-f539cc6acd28/lib/mongoid/persistence_context.rb:111:in `client'
from /home/vagrant/.bundle/ruby/2.3.0/mongoid-f539cc6acd28/lib/mongoid/persistence_context.rb:71:in `collection'
from /home/vagrant/.bundle/ruby/2.3.0/mongoid-f539cc6acd28/lib/mongoid/clients/options.rb:73:in `collection'
from /home/vagrant/.bundle/ruby/2.3.0/mongoid-f539cc6acd28/lib/mongoid/contextual/mongo.rb:324:in `initialize'
from /home/vagrant/.bundle/ruby/2.3.0/mongoid-f539cc6acd28/lib/mongoid/contextual.rb:53:in `new'
from /home/vagrant/.bundle/ruby/2.3.0/mongoid-f539cc6acd28/lib/mongoid/contextual.rb:53:in `create_context'
from /home/vagrant/.bundle/ruby/2.3.0/mongoid-f539cc6acd28/lib/mongoid/contextual.rb:35:in `context'
from /home/vagrant/.bundle/ruby/2.3.0/mongoid-f539cc6acd28/lib/mongoid/contextual.rb:20:in `first'
from (irb):1
from /usr/local/rvm/gems/ruby-2.3.1/gems/railties-5.0.0/lib/rails/commands/console.rb:65:in `start'
from /usr/local/rvm/gems/ruby-2.3.1/gems/railties-5.0.0/lib/rails/commands/console_helper.rb:9:in `start'
from /usr/local/rvm/gems/ruby-2.3.1/gems/railties-5.0.0/lib/rails/commands/commands_tasks.rb:78:in `console'
from /usr/local/rvm/gems/ruby-2.3.1/gems/railties-5.0.0/lib/rails/commands/commands_tasks.rb:49:in `run_command!'
from /usr/local/rvm/gems/ruby-2.3.1/gems/railties-5.0.0/lib/rails/commands.rb:18:in `<top (required)>'
from /usr/local/rvm/gems/ruby-2.3.1/gems/activesupport-5.0.0/lib/active_support/dependencies.rb:293:in `require'
from /usr/local/rvm/gems/ruby-2.3.1/gems/activesupport-5.0.0/lib/active_support/dependencies.rb:293:in `block in require'
from /usr/local/rvm/gems/ruby-2.3.1/gems/activesupport-5.0.0/lib/active_support/dependencies.rb:259:in `load_dependency'
from /usr/local/rvm/gems/ruby-2.3.1/gems/activesupport-5.0.0/lib/active_support/dependencies.rb:293:in `require'
from /vagrant/sample_mongoid/bin/rails:9:in `<top (required)>'
from /usr/local/rvm/gems/ruby-2.3.1/gems/activesupport-5.0.0/lib/active_support/dependencies.rb:287:in `load'
from /usr/local/rvm/gems/ruby-2.3.1/gems/activesupport-5.0.0/lib/active_support/dependencies.rb:287:in `block in load'
from /usr/local/rvm/gems/ruby-2.3.1/gems/activesupport-5.0.0/lib/active_support/dependencies.rb:259:in `load_dependency'
from /usr/local/rvm/gems/ruby-2.3.1/gems/activesupport-5.0.0/lib/active_support/dependencies.rb:287:in `load'
from /usr/local/rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from /usr/local/rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from -e:1:in `<main>'
我在这里尝试了其他类似问题中的一些建议,但没有解决。
解决了我的问题是将rspec添加到我的Gemfile中:
gem 'rspec-rails', '~> 3.4'
我的问题是:如果我不想使用rspec,而是单位测试,我该怎么做?或者它是强制使用rspec与mongoid?
UPDATE1:
我的mongoyd.yml:
development:
clients:
default:
database: sample_mongoid_development
hosts:
- localhost:27017
options:
consistency: :strong
options:
raise_not_found_error: false
test:
clients:
default:
database: sample_mongoid_test
hosts:
- localhost:27017
options:
read:
mode: :primary
max_pool_size: 1