Search code examples
rubymongoidrspec-rails

Mongoid and Rspec error Mongo::Error::NoServerAvailable:


When I am starting my Rspec test, I get this error:

  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

I am really confused about it.

My mongo server is running too.

My 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

Any idea why my mongo won't work?


Solution

  • Found the answer

    My config file had mistake so I change it to :

    test: clients: default: database: test hosts: - 127.0.0.1:27017 options: read: mode: :primary max_pool_size: 1 log_level: :debug