ruby on rails - Mongoid Model.find fails in spec, but works in app -


next line:

  user.find(session[:user_id]) 

in session[:user_id] defined:

  session[:user_id] = user.id 

in spec works fine , returns user model, in real app (development mode) fails:

moped: 127.0.0.1:27017 query        database=rails_api_development collection=users selector={"_id"=>{"$oid"=>bson::objectid('533c3958616c641142010000')}} flags=[] limit=0 skip=0 batch_size=nil fields=nil runtime: 1.0680ms completed 500 internal server error in 15ms  moped::errors::queryfailure - operation: #<moped::protocol::query     @length=89 @request_id=2 @response_to=0 @op_code=2004 @flags=[] @full_collection_name="rails_api_development.users" @skip=0 @limit=0 @selector={"_id"=>{"$oid"=>bson::objectid('533c3958616c641142010000')}} @fields=nil>     failed error 10068: "invalid operator: $oid"  see https://github.com/mongodb/mongo/blob/master/docs/errors.md details error.:   moped (2.0.0.rc1) lib/moped/operation/read.rb:50:in `block in execute'   moped (2.0.0.rc1) lib/moped/node.rb:594:in `block (2 levels) in flush' 

full log

app works when change find to:

  user.find(session[:user_id]['$oid']) 

but spec fails with:

   1) applicationcontroller current_user when current_user nil , user_id stores in session finds , returns db  failure/error: expect(subject.send(:current_user)).to eq user  nomethoderror:    undefined method `[]' bson::objectid('533fae9e616c6464a4010000'):bson::objectid  # ./app/controllers/application_controller.rb:7:in `current_user'  # ./spec/controllers/application_controller_spec.rb:22:in `block (5 levels) in <top (required)>' 

in spec i'm working real db, database_cleaner. so, guess same (but not)

my gemfile

i've tried make to_s, to_json user_id according this, add mongoid initialize file with this, tried cahnge multi_json moped versions - didn't help.

i have application controller spec there

i barely hope can help, taking account results of previous such tricky questions, anyway, in advance!

update: failing test:

context "when current_user nil"   context "and user_id stores in session"     let(:user) { create(:user) }     before { allow(subject).to receive(:session).and_return({ user_id: user.id }) }     before { allow(user).to receive(:find).and_return(user) }      "finds , returns db"       expect(user).to receive(:find) >     expect(subject.send(:current_user)).to eq user     end   end 

i had similar problem earlier, problem json representation of bson::objectid in session. found, calling to_s solved problem me. need make sure session contains id in string format. whenever assign user id session, call to_s. try make following changes:

change current_user= in applicationcontroller to:

def current_user=(user)   @current_user = user   session[:user_id] = user.try(:id).try(:to_s) end 

change spec to:

context "and user_id stores in session"   let(:user) { create(:user) }   before { allow(subject).to receive(:session).and_return({ user_id: user.id.to_s }) }   before { allow(user).to receive(:find).and_return(user) }    "finds , returns db"     expect(user).to receive(:find)     expect(subject.send(:current_user)).to eq user   end end 

Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -