Javascript 摩卡,诺德斯承诺测试罐';我没有完成,因为没有完成
我正在尝试运行承诺的测试,但测试失败,因为它超过了超时限制,并建议确保我已经完成了子句 这是我的测试代码的一部分:Javascript 摩卡,诺德斯承诺测试罐';我没有完成,因为没有完成,javascript,node.js,mocha.js,chai,Javascript,Node.js,Mocha.js,Chai,我正在尝试运行承诺的测试,但测试失败,因为它超过了超时限制,并建议确保我已经完成了子句 这是我的测试代码的一部分: $configurations .updateConfiguration(configurations_driver.NOT_VALID_MODEL) //invalid model .then(function () { done(new Error("Expected INVALID_MODEL error but got OK"));
$configurations
.updateConfiguration(configurations_driver.NOT_VALID_MODEL) //invalid model
.then(function () {
done(new Error("Expected INVALID_MODEL error but got OK"));
}, function (error) {
chai.assert.isNotNull(error);
chai.expect(error.message).to.be.eq("INVALID_MODEL_ERROR");
chai.expect(error.kind).to.be.eq("ERROR_KIND");
chai.expect(error.path).to.be.eq("ERROR_PATH");
done();
})
.catch(done);
});
正如您所看到的,我有所有的done子句,所以我不知道我是否在测试中遗漏了什么或者结构是错误的。摩卡支持测试承诺,而不使用
done
,只要您返回承诺
const expect = chai.expect
it('should error', function(){
return $configurations
.updateConfiguration(configurations_driver.NOT_VALID_MODEL) //invalid model
.then(()=> { throw new Error("Expected INVALID_MODEL error but got OK")})
.catch(error => {
expect(error).to.not.be.null;
expect(error.message).to.equal("INVALID_MODEL_ERROR");
expect(error.kind).to.equal("ERROR_KIND");
expect(error.path).to.equal("ERROR_PATH");
})
})
另外,还要注意使承诺测试更像标准的断言/期望
chai.should()
chai.use(require('chai-as-promised'))
it('should error', function(){
return $configurations
.updateConfiguration(configurations_driver.NOT_VALID_MODEL)
.should.be.rejectedWith(/INVALID_MODEL_ERROR/)
})
在Node 7.6+环境中或您有/也可以使用/promise处理程序
it('should error', async function(){
try {
await $configurations.updateConfiguration(configurations_driver.NOT_VALID_MODEL)
throw new Error("Expected INVALID_MODEL error but got OK")})
} catch (error) {
expect(error).to.not.be.null;
expect(error.message).to.equal("INVALID_MODEL_ERROR");
expect(error.kind).to.equal("ERROR_KIND");
expect(error.path).to.equal("ERROR_PATH");
}
})
只要您返回承诺,Mocha支持在不使用done
的情况下测试承诺
const expect = chai.expect
it('should error', function(){
return $configurations
.updateConfiguration(configurations_driver.NOT_VALID_MODEL) //invalid model
.then(()=> { throw new Error("Expected INVALID_MODEL error but got OK")})
.catch(error => {
expect(error).to.not.be.null;
expect(error.message).to.equal("INVALID_MODEL_ERROR");
expect(error.kind).to.equal("ERROR_KIND");
expect(error.path).to.equal("ERROR_PATH");
})
})
另外,还要注意使承诺测试更像标准的断言/期望
chai.should()
chai.use(require('chai-as-promised'))
it('should error', function(){
return $configurations
.updateConfiguration(configurations_driver.NOT_VALID_MODEL)
.should.be.rejectedWith(/INVALID_MODEL_ERROR/)
})
在Node 7.6+环境中或您有/也可以使用/promise处理程序
it('should error', async function(){
try {
await $configurations.updateConfiguration(configurations_driver.NOT_VALID_MODEL)
throw new Error("Expected INVALID_MODEL error but got OK")})
} catch (error) {
expect(error).to.not.be.null;
expect(error.message).to.equal("INVALID_MODEL_ERROR");
expect(error.kind).to.equal("ERROR_KIND");
expect(error.path).to.equal("ERROR_PATH");
}
})