Mongoid和Rspec错误Mongo :: Error :: NoServerAvailable:

时间:2019-01-16 03:03:14

标签: ruby mongoid rspec-rails

当我开始Rspec测试时,出现此错误:

  1) User checks if the user is created
     Failure/Error: expect{ @user_test = create(:user) }.to change { User.count }

     Mongo::Error::NoServerAvailable:
       No server is available matching preference: #<Mongo::ServerSelector::Primary:0x70316515164800 tag_sets=[] max_staleness=nil> using server_selection_timeout=30 and local_threshold=0.015
     # /Users/aliceguillaume/.rvm/gems/ruby-2.3.4/gems/mongo-2.5.0/lib/mongo/server_selector/selectable.rb:115:in `select_server'
     # /Users/aliceguillaume/.rvm/gems/ruby-2.3.4/gems/mongo-2.5.0/lib/mongo/collection/view/readable.rb:139:in `block in count'
     # /Users/aliceguillaume/.rvm/gems/ruby-2.3.4/gems/mongo-2.5.0/lib/mongo/retryable.rb:44:in `read_with_retry'
     # /Users/aliceguillaume/.rvm/gems/ruby-2.3.4/gems/mongo-2.5.0/lib/mongo/collection/view/readable.rb:138:in `count'
     # /Users/aliceguillaume/.rvm/gems/ruby-2.3.4/gems/mongoid-5.2.1/lib/mongoid/contextual/mongo.rb:70:in `block in count'
     # /Users/aliceguillaume/.rvm/gems/ruby-2.3.4/gems/mongoid-5.2.1/lib/mongoid/contextual/mongo.rb:504:in `try_cache'
     # /Users/aliceguillaume/.rvm/gems/ruby-2.3.4/gems/mongoid-5.2.1/lib/mongoid/contextual/mongo.rb:70:in `count'
     # /Users/aliceguillaume/.rvm/gems/ruby-2.3.4/gems/mongoid-5.2.1/lib/mongoid/contextual.rb:20:in `count'
     # /Users/aliceguillaume/.rvm/gems/ruby-2.3.4/gems/mongoid-5.2.1/lib/mongoid/findable.rb:55:in `count'
     # ./spec/models/user_spec.rb:11:in `block (3 levels) in <top (required)>'
     # ./spec/models/user_spec.rb:11:in `block (2 levels) in <top (required)>'

Finished in 30.14 seconds (files took 8.39 seconds to load)
1 example, 1 failure

Failed examples:

rspec ./spec/models/user_spec.rb:10 # User checks if the user is created

我真的很困惑。

我的mongo服务器也正在运行。

我的user_spec.rb:

require 'spec_helper'
require 'rails_helper'
require 'database_cleaner'


DatabaseCleaner.strategy = :truncation

RSpec.describe User, :type => :model do

  it "checks if the user is created" do
    expect{ @user_test = create(:user) }.to change { User.count }
  end

end

有人知道为什么我的mongo无法正常工作吗?

1 个答案:

答案 0 :(得分:0)

找到答案

我的配置文件有误,所以我将其更改为:

测试:   客户:     默认:       数据库:测试       主持人:         -127.0.0.1:27017       选项:         读:           模式::primary         max_pool_size:1   log_level::调试