My specs fail after upgrade to 3.0.0
tbjers opened this issue · 11 comments
It appears that mongoid-rspec 2.3.0.beta has been yanked, so my projects are no longer able to deploy.
I tried upgrading to mongoid 5.0.0 and mongoid-rspec 3.0.0. My Rails app works fine. However, all my rspec tests that involve my user model fail (thus causing CI to not deploy) with the following errors:
$ rspec spec/models/user_spec.rb
FFF..F
Failures:
1) User has a valid factory
Failure/Error: expect(FactoryGirl.create(:user)).to be_valid
KeyError:
key not found: "primary"
# ./app/models/user.rb:68:in `block in generate_authenication_token'
# ./app/models/user.rb:66:in `loop'
# ./app/models/user.rb:66:in `generate_authenication_token'
# ./app/models/user.rb:56:in `ensure_authentication_token!'
# ./spec/models/user_spec.rb:6:in `block (2 levels) in <top (required)>'
2) User has an authentication_token
Failure/Error: expect(FactoryGirl.create(:user).authentication_token).not_to be_empty
KeyError:
key not found: "primary"
# ./app/models/user.rb:68:in `block in generate_authenication_token'
# ./app/models/user.rb:66:in `loop'
# ./app/models/user.rb:66:in `generate_authenication_token'
# ./app/models/user.rb:56:in `ensure_authentication_token!'
# ./spec/models/user_spec.rb:9:in `block (2 levels) in <top (required)>'
3) User has encrypted_password
Failure/Error: expect(FactoryGirl.create(:user).encrypted_password).not_to be_empty
KeyError:
key not found: "primary"
# ./app/models/user.rb:68:in `block in generate_authenication_token'
# ./app/models/user.rb:66:in `loop'
# ./app/models/user.rb:66:in `generate_authenication_token'
# ./app/models/user.rb:56:in `ensure_authentication_token!'
# ./spec/models/user_spec.rb:12:in `block (2 levels) in <top (required)>'
4) User does not allow duplicate emails
Failure/Error: expect(FactoryGirl.create(:user, email: email)).to be_valid
KeyError:
key not found: "primary"
# ./app/models/user.rb:68:in `block in generate_authenication_token'
# ./app/models/user.rb:66:in `loop'
# ./app/models/user.rb:66:in `generate_authenication_token'
# ./app/models/user.rb:56:in `ensure_authentication_token!'
# ./spec/models/user_spec.rb:22:in `block (2 levels) in <top (required)>'
Finished in 1.69 seconds (files took 3.05 seconds to load)
6 examples, 4 failures
Failed examples:
rspec ./spec/models/user_spec.rb:5 # User has a valid factory
rspec ./spec/models/user_spec.rb:8 # User has an authentication_token
rspec ./spec/models/user_spec.rb:11 # User has encrypted_password
rspec ./spec/models/user_spec.rb:20 # User does not allow duplicate emails
Gemfile:
source 'https://rubygems.org'
ruby '2.2.2'
gem 'rails', '4.2.3'
gem 'jbuilder', '~> 2.0'
gem 'sdoc', '~> 0.4.0', group: :doc
gem 'bcrypt', '~> 3.1.7'
gem 'mongoid', '~> 5.0.0'
gem 'devise'
gem 'grape'
gem 'grape-entity'
gem 'rack-contrib'
gem 'grape-swagger'
gem 'grape-swagger-rails'
gem 'rack-cors', require: 'rack/cors'
gem 'sucker_punch', '~> 1.0'
gem 'rollbar', '~> 2.1.1'
gem 'rails_12factor'
group :development, :test do
gem 'rspec-rails'
gem 'mongoid-rspec', '~> 3.0.0'
gem 'factory_girl_rails'
end
group :development do
gem 'spring'
gem 'quiet_assets'
end
group :test do
gem 'faker'
gem 'coveralls', require: false
gem 'rubocop', require: false
gem 'simplecov', require: false
end
gem 'puma'
app/models/user.rb:
class User
include Mongoid::Document
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
## Database authenticatable
field :email, type: String, default: ''
field :encrypted_password, type: String, default: ''
## Recoverable
field :reset_password_token, type: String
field :reset_password_sent_at, type: Time
## Rememberable
field :remember_created_at, type: Time
## Trackable
field :sign_in_count, type: Integer, default: 0
field :current_sign_in_at, type: Time
field :last_sign_in_at, type: Time
field :current_sign_in_ip, type: String
field :last_sign_in_ip, type: String
## Confirmable
# field :confirmation_token, type: String
# field :confirmed_at, type: Time
# field :confirmation_sent_at, type: Time
# field :unconfirmed_email, type: String # Only if using reconfirmable
## Lockable
# field :failed_attempts, type: Integer, default: 0 # Only if lock strategy is :failed_attempts
# field :unlock_token, type: String # Only if unlock strategy is :email or :both
# field :locked_at, type: Time
field :authentication_token, type: String
before_save :ensure_authentication_token!
before_create :set_default_role
validates :email, presence: true
has_many :campaigns
has_many :adventures
has_many :hooks
has_many :ideas
has_many :locations
has_many :stories
belongs_to :role
index({ email: 1 }, unique: true, drop_dups: false)
index({ authentication_token: 1 }, unique: true, drop_dups: false)
def ensure_authentication_token!
self.authentication_token ||= generate_authenication_token
end
def reset_authentication_token
generate_authenication_token
end
private
def generate_authenication_token
loop do
token = Devise.friendly_token
break token unless User.find_by(authentication_token: token)
end
end
def set_default_role
self.role ||= Role.find_by(name: 'registered')
end
end
spec/factories/users.rb:
require 'faker'
FactoryGirl.define do
factory :user do
email { Faker::Internet.email }
password { Faker::Internet.password }
end
end
spec/models/user_spec.rb:
require 'rails_helper'
require 'faker'
RSpec.describe User, type: :model do
it 'has a valid factory' do
expect(FactoryGirl.create(:user)).to be_valid
end
it 'has an authentication_token' do
expect(FactoryGirl.create(:user).authentication_token).not_to be_empty
end
it 'has encrypted_password' do
expect(FactoryGirl.create(:user).encrypted_password).not_to be_empty
end
it 'does not validate with a short password' do
expect(FactoryGirl.build(:user, password: Faker::Internet.password(4))).not_to be_valid
end
it 'does not validate with a too long password' do
expect(FactoryGirl.build(:user, password: Faker::Internet.password(90))).not_to be_valid
end
it 'does not allow duplicate emails' do
email = Faker::Internet.email
expect(FactoryGirl.create(:user, email: email)).to be_valid
expect(FactoryGirl.build(:user, email: email)).not_to be_valid
end
end
Any assistance with this would be greatly appreciated!
Hey @tbjers could you post the full stacktrace? Running rspec
with -b
or --backtrace
you get it.
I don't see a single line that's involving mongoid-rspec. You're not using mongoid-rspec matchers in showed code.
@rodrigopinto, what's weird is that I don't get these errors when I just access the app API via Swagger or raw curl calls. Here's the full stacktrace for the first spec failure:
7) User has a valid factory
Failure/Error: expect(FactoryGirl.create(:user)).to be_valid
KeyError:
key not found: "primary"
# /usr/local/opt/rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/mongo-2.1.0/lib/mongo/server_selector.rb:68:in `fetch'
# /usr/local/opt/rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/mongo-2.1.0/lib/mongo/server_selector.rb:68:in `get'
# /usr/local/opt/rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/mongo-2.1.0/lib/mongo/client.rb:188:in `read_preference'
# /usr/local/opt/rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/mongo-2.1.0/lib/mongo/collection.rb:104:in `read_preference'
# /usr/local/opt/rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/mongo-2.1.0/lib/mongo/collection/view/readable.rb:411:in `default_read'
# /usr/local/opt/rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/mongo-2.1.0/lib/mongo/collection/view/readable.rb:300:in `read'
# /usr/local/opt/rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/mongo-2.1.0/lib/mongo/collection/view/iterable.rb:40:in `block in each'
# /usr/local/opt/rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/mongo-2.1.0/lib/mongo/retryable.rb:43:in `call'
# /usr/local/opt/rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/mongo-2.1.0/lib/mongo/retryable.rb:43:in `read_with_retry'
# /usr/local/opt/rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/mongo-2.1.0/lib/mongo/collection/view/iterable.rb:39:in `each'
# /usr/local/opt/rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/mongoid-5.0.0/lib/mongoid/query_cache.rb:207:in `each'
# /usr/local/opt/rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/mongoid-5.0.0/lib/mongoid/contextual/mongo.rb:252:in `first'
# /usr/local/opt/rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/mongoid-5.0.0/lib/mongoid/contextual/mongo.rb:252:in `find_first'
# /usr/local/opt/rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/mongoid-5.0.0/lib/mongoid/contextual.rb:20:in `find_first'
# /usr/local/opt/rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/mongoid-5.0.0/lib/mongoid/findable.rb:113:in `find_by'
# ./app/models/user.rb:68:in `block in generate_authenication_token'
# ./app/models/user.rb:66:in `loop'
# ./app/models/user.rb:66:in `generate_authenication_token'
# ./app/models/user.rb:56:in `ensure_authentication_token!'
# /usr/local/opt/rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/activesupport-4.2.3/lib/active_support/callbacks.rb:430:in `block in make_lambda'
# /usr/local/opt/rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/activesupport-4.2.3/lib/active_support/callbacks.rb:162:in `call'
# /usr/local/opt/rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/activesupport-4.2.3/lib/active_support/callbacks.rb:162:in `block in halting'
# /usr/local/opt/rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/activesupport-4.2.3/lib/active_support/callbacks.rb:502:in `call'
# /usr/local/opt/rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/activesupport-4.2.3/lib/active_support/callbacks.rb:502:in `block in call'
# /usr/local/opt/rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/activesupport-4.2.3/lib/active_support/callbacks.rb:502:in `each'
# /usr/local/opt/rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/activesupport-4.2.3/lib/active_support/callbacks.rb:502:in `call'
# /usr/local/opt/rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/activesupport-4.2.3/lib/active_support/callbacks.rb:88:in `run_callbacks'
# /usr/local/opt/rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/mongoid-5.0.0/lib/mongoid/interceptable.rb:138:in `run_callbacks'
# /usr/local/opt/rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/mongoid-5.0.0/lib/mongoid/persistable/creatable.rb:116:in `prepare_insert'
# /usr/local/opt/rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/mongoid-5.0.0/lib/mongoid/persistable/creatable.rb:23:in `insert'
# /usr/local/opt/rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/mongoid-5.0.0/lib/mongoid/persistable/savable.rb:23:in `save'
# /usr/local/opt/rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/mongoid-5.0.0/lib/mongoid/persistable/savable.rb:44:in `save!'
# /usr/local/opt/rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/factory_girl-4.5.0/lib/factory_girl/configuration.rb:14:in `block in initialize'
# /usr/local/opt/rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/factory_girl-4.5.0/lib/factory_girl/evaluation.rb:15:in `[]'
# /usr/local/opt/rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/factory_girl-4.5.0/lib/factory_girl/evaluation.rb:15:in `create'
# /usr/local/opt/rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/factory_girl-4.5.0/lib/factory_girl/strategy/create.rb:12:in `block in result'
# /usr/local/opt/rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/factory_girl-4.5.0/lib/factory_girl/strategy/create.rb:9:in `tap'
# /usr/local/opt/rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/factory_girl-4.5.0/lib/factory_girl/strategy/create.rb:9:in `result'
# /usr/local/opt/rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/factory_girl-4.5.0/lib/factory_girl/factory.rb:42:in `run'
# /usr/local/opt/rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/factory_girl-4.5.0/lib/factory_girl/factory_runner.rb:23:in `block in run'
# /usr/local/opt/rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/activesupport-4.2.3/lib/active_support/notifications.rb:166:in `instrument'
# /usr/local/opt/rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/factory_girl-4.5.0/lib/factory_girl/factory_runner.rb:22:in `run'
# /usr/local/opt/rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/factory_girl-4.5.0/lib/factory_girl/strategy_syntax_method_registrar.rb:20:in `block in define_singular_strategy_method'
# ./spec/models/user_spec.rb:6:in `block (2 levels) in <top (required)>'
# /usr/local/Cellar/rbenv/0.4.0/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rspec-core-3.3.2/lib/rspec/core/example.rb:206:in `instance_exec'
# /usr/local/Cellar/rbenv/0.4.0/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rspec-core-3.3.2/lib/rspec/core/example.rb:206:in `block in run'
# /usr/local/Cellar/rbenv/0.4.0/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rspec-core-3.3.2/lib/rspec/core/example.rb:430:in `block in with_around_and_singleton_context_hooks'
# /usr/local/Cellar/rbenv/0.4.0/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rspec-core-3.3.2/lib/rspec/core/example.rb:388:in `block in with_around_example_hooks'
# /usr/local/Cellar/rbenv/0.4.0/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rspec-core-3.3.2/lib/rspec/core/hooks.rb:478:in `block in run'
# /usr/local/Cellar/rbenv/0.4.0/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rspec-core-3.3.2/lib/rspec/core/hooks.rb:618:in `block in run_around_example_hooks_for'
# /usr/local/Cellar/rbenv/0.4.0/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rspec-core-3.3.2/lib/rspec/core/example.rb:273:in `call'
# /usr/local/Cellar/rbenv/0.4.0/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rspec-core-3.3.2/lib/rspec/core/example.rb:273:in `call'
# /usr/local/opt/rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rspec-rails-3.3.3/lib/rspec/rails/adapters.rb:127:in `block (2 levels) in <module:MinitestLifecycleAdapter>'
# /usr/local/Cellar/rbenv/0.4.0/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rspec-core-3.3.2/lib/rspec/core/example.rb:378:in `instance_exec'
# /usr/local/Cellar/rbenv/0.4.0/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rspec-core-3.3.2/lib/rspec/core/example.rb:378:in `instance_exec'
# /usr/local/Cellar/rbenv/0.4.0/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rspec-core-3.3.2/lib/rspec/core/hooks.rb:389:in `execute_with'
# /usr/local/Cellar/rbenv/0.4.0/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rspec-core-3.3.2/lib/rspec/core/hooks.rb:620:in `block (2 levels) in run_around_example_hooks_for'
# /usr/local/Cellar/rbenv/0.4.0/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rspec-core-3.3.2/lib/rspec/core/example.rb:273:in `call'
# /usr/local/Cellar/rbenv/0.4.0/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rspec-core-3.3.2/lib/rspec/core/example.rb:273:in `call'
# /usr/local/Cellar/rbenv/0.4.0/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rspec-core-3.3.2/lib/rspec/core/hooks.rb:621:in `run_around_example_hooks_for'
# /usr/local/Cellar/rbenv/0.4.0/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rspec-core-3.3.2/lib/rspec/core/hooks.rb:478:in `run'
# /usr/local/Cellar/rbenv/0.4.0/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rspec-core-3.3.2/lib/rspec/core/example.rb:388:in `with_around_example_hooks'
# /usr/local/Cellar/rbenv/0.4.0/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rspec-core-3.3.2/lib/rspec/core/example.rb:430:in `with_around_and_singleton_context_hooks'
# /usr/local/Cellar/rbenv/0.4.0/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rspec-core-3.3.2/lib/rspec/core/example.rb:203:in `run'
# /usr/local/Cellar/rbenv/0.4.0/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rspec-core-3.3.2/lib/rspec/core/example_group.rb:559:in `block in run_examples'
# /usr/local/Cellar/rbenv/0.4.0/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rspec-core-3.3.2/lib/rspec/core/example_group.rb:555:in `map'
# /usr/local/Cellar/rbenv/0.4.0/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rspec-core-3.3.2/lib/rspec/core/example_group.rb:555:in `run_examples'
# /usr/local/Cellar/rbenv/0.4.0/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rspec-core-3.3.2/lib/rspec/core/example_group.rb:521:in `run'
# /usr/local/Cellar/rbenv/0.4.0/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb:115:in `block (3 levels) in run_specs'
# /usr/local/Cellar/rbenv/0.4.0/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb:115:in `map'
# /usr/local/Cellar/rbenv/0.4.0/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb:115:in `block (2 levels) in run_specs'
# /usr/local/Cellar/rbenv/0.4.0/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rspec-core-3.3.2/lib/rspec/core/configuration.rb:1627:in `with_suite_hooks'
# /usr/local/Cellar/rbenv/0.4.0/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb:114:in `block in run_specs'
# /usr/local/Cellar/rbenv/0.4.0/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rspec-core-3.3.2/lib/rspec/core/reporter.rb:77:in `report'
# /usr/local/Cellar/rbenv/0.4.0/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb:113:in `run_specs'
# /usr/local/Cellar/rbenv/0.4.0/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb:89:in `run'
# /usr/local/Cellar/rbenv/0.4.0/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb:73:in `run'
# /usr/local/Cellar/rbenv/0.4.0/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb:41:in `invoke'
# /usr/local/opt/rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rspec-core-3.3.2/exe/rspec:4:in `<top (required)>'
# /usr/local/opt/rbenv/versions/2.2.2/bin/rspec:23:in `load'
# /usr/local/opt/rbenv/versions/2.2.2/bin/rspec:23:in `<main>'
@tbjers The error is happening on your factory
when it is calling the callback to generate authentication token. It's not related to mongoid-rspec and seems to be happening on User.find_by(authentication_token: token)
.
Take a look at your configuration. Try to create a User on a rails console sandbox based on your factory data to see what happens, maybe it helps you.
So why does this not happen with mongoid-rspec 2.3.0.beta and mongoid
5.0.0.beta?
On Mon, Sep 28, 2015, 15:01 Rodrigo Pinto notifications@github.com wrote:
—
Reply to this email directly or view it on GitHub
#159 (comment)
.
@tbjers Why do you keep insisting that problem is related to mongoid-rspec
?
mongoid-rspec
only adds extra matchers torspec
- you don't use any of those in showed code
- backtrace shows that you have an exception in
mongoid
itself
Hmm. Alright, you are correct. I'll file the issue in that repo instead.
Thank you.
On Mon, Sep 28, 2015, 16:27 Alexander Karmes notifications@github.com
wrote:
@tbjers https://github.com/tbjers Why do you keep insisting that
problem with "mongoid-rspec"?
- mongoid-rspec only adds extra matchers to rspec
- you don't use any of those in showed code
- backtrace shows that you have an exception in mongoid itself
—
Reply to this email directly or view it on GitHub
#159 (comment)
.
@tbjers remove this option from your mongoid.yml configuration:
options:
read:
mode: primary
It works for me.
@rjurado01 Thanks. I did the same eventually, just forgot to update this ticket.
@rjurado01 👍 it also works for me!
yep removing the following config worked eventually:
read:
mode: primary