柴作为承诺测试拒绝超时

chai as promised test rejection timeout

我应该如何测试拒绝:

return new Promise(function(resolve, reject){
            models.users.find({
                where: {
                    email: email
                }
            }).then(function(result){
                if(!result)
                    throw 'Invalid password'
            }).catch(function(err){
                reject(err);
            });
        });

在我的测试中:

it('should be rejected', function(){
            let data= { req: {
                email: 'alvin@.oc',
            }};

            return User.authUser(data).should.be.rejected;
        });

它应该被拒绝并出现错误 'Invalid password',但我收到错误:

 Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure
it resolves.

您可以抛出错误并测试错误消息的内容。 你不需要拒绝承诺,因为异常拒绝承诺,你不应该把它包装在另一个承诺中:

   return models.users.find({
            where: {
                email: email
            }
        }).then(function(result){
            if(!result)
                throw new Error('Invalid password');
        });

在你的测试中:

return User.authUser(data).should.be.rejectedWith(Error).and.eventually.have.property("message").equal('Invalid password');