“done()多次调用”moochaoose with mocha

时间:2015-10-11 09:45:16

标签: javascript node.js mongodb mocha

See debugger output here

var expect = require('chai').expect  
var User = require('../../app/models/index.js').User  


describe('Model User', function(){  
  it('add a user', function(done){  
    var user = new User({  
      uname: 'cx',  
      password: 'cx'  
    })  

   user.save()    //Model#save()  return Promise
    .then(function(user,effectNum){  
       done()   //success state
    })
    .catch(function(error){
      done(error)  //fail state
    })
 })
})

使用Chai,Mocha,Moongoose 我在学习moongodb时测试我的代码。似乎是正确的 但是我收到了一个错误:

   0 passing (2m)
   2 failing

   1) Model User add a user:
     Error: done() called multiple times

   2) Model User add a user:
     Error: done() called multiple times

为什么多次调用

2 个答案:

答案 0 :(得分:2)

describe('Model User', function(){


     it('add a user', function(done){
    var user = new User({
      uname: 'cx',
      password: 'cx'
    })

    user.save()
        .then(function(){
          assert(false)
        }).then(done,done)

  })
})

//or
describe('Model User', function(){
  it('add a user', function(){
    var user = new User({
      uname: 'cx',
      password: 'cx'
    })

   return  user.save()
        .then(function(){
          assert(false)
        })

  })
})

Mocha支持Promise ^ _ ^,我的错误

答案 1 :(得分:0)

done()回调接受错误,因此您可以尝试直接使用它:

it('should add a user', function(done) {
      var user = new User({  
          uname: 'cx',  
          password: 'cx'  
      }); 

      user.save(function(err) {
        if (err) throw err;
        done();
      });
});