title: How to get data passed to mongoose schema constructor
body: <p>I am testing my application and need to verify that mongoose schema constructor is called with correct data.</p>
  
  <p>let's say I do this:</p>
  
  <pre><code>const UserData = new User(user)
  console.log(UserData.contructor.args)
  </code></pre>
  
  <p>I would expect log of the <code>user</code> object.
  Probably the data is passed to constructor of mongoose schema?</p>
  
  <p>Can some one please advise me how to access it?</p>
  
  <p>Here is specific case I am trying to solve.</p>
  
  <pre><code>export const signup = async (req, res, next) =&gt; {
      try {
  
          //if user object is missing return error
          if (!req.body.user) 
              return next(boom.unauthorized('No user data received.'))        
  
          //get user data    
          const user                                      = req.body.user,
          { auth: { local: { password, password_2 } } }   = user        
  
          //check if both passwords match
          if (password !== password_2)
              return next(boom.unauthorized('Passwords do not match.'))
  
          //check if password is valid
          if (!Password.validate(password)) {          
              const errorData = Password.validate(password, { list: true })
              return next(boom.notAcceptable('Invalid password.', errorData))
          }    
  
          //creates new mongo user
          const UserData = new User(user)
  
          //sets user password hash   
          UserData.setPassword(password)
  
          //saves user to database
          await UserData.save()        
  
          //returns new users authorization data
          return res.json({ user: UserData.toAuthJSON() })
  
      } catch(err) {
  
          //if mongo validation error return callback with error       
          if(err.name === 'ValidationError') {
              return next(boom.unauthorized(err.message))
          }
  
          // all other server errors           
          return next(boom.badImplementation('Something went wrong', err))
      }
  
  }
  </code></pre>
  
  <p>And part of test:</p>
  
  <pre><code>describe('Success', () =&gt; {
              it('Should create new instance of User with request data', async () =&gt; {
                  const   req             = { body },
                          res             = {},
                          local           = { password: '1aaaBB', password_2: '1aaaBB'},
                          constructorStub = sandbox.stub(User.prototype, 'constructor')                
  
                  req.body.user.auth.local    = {...local}
  
                  await signup(req, res, next)
  
                  expect(constructorStub.calledOnceWith({...req.body.user})).to.be.true
  
              })                
          })
  </code></pre>
  
  <p>EDIT: I can verify that is is called with <code>expect(constructorStub.calledOnce).to.be.true</code></p>
  
  <p>Just can't get to verify data passed.</p>