摩卡,nodejs 承诺测试无法完成,因为缺少完成
Mocha, nodejs promise test can't finish because lack of done
我正在尝试运行 promise 的测试,但测试失败,认为它超过了超时限制,并建议确保我有完成的条款。
这是我的测试代码的一部分:
$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);
});
如您所见,我在其中包含所有已完成的子句,所以我不知道我是否在测试中遗漏了某些内容或结构有误。
Mocha 支持在不使用 done
的情况下测试承诺,只要您 return
承诺。
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-as-promised
以使 promise 测试更像标准 chai assertions/expectations。
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+ 环境或您有 babel/babel-register you can also make use of the async
/await
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");
}
})
我正在尝试运行 promise 的测试,但测试失败,认为它超过了超时限制,并建议确保我有完成的条款。
这是我的测试代码的一部分:
$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);
});
如您所见,我在其中包含所有已完成的子句,所以我不知道我是否在测试中遗漏了某些内容或结构有误。
Mocha 支持在不使用 done
的情况下测试承诺,只要您 return
承诺。
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-as-promised
以使 promise 测试更像标准 chai assertions/expectations。
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+ 环境或您有 babel/babel-register you can also make use of the async
/await
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");
}
})