查看其他问题,无法真正找到问题的原因。我正在尝试使用摩卡进行测试。
it("Should not do the work",function(done) {
axios
.post("x/y",{ id:a2 })
.then(function(res) {
assert(false,"Should not do the work");
done();
})
.catch(function(res) {
assert.equal(HttpStatus.CONFLICT,res.status);
done();
});
});
it("Should do the work",function(done) {
axios
.post("/x/y",{ id: a1 })
.then(function(res) {
done();
})
.catch(done);
});
结果是:
√ Should not do the work (64ms)
1) Should do the work
1 passing (20s)
1 failing
1) Error: Timeout of 20000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
增加超时不起作用。
不要忘记,您只需在摩卡中return
一个承诺,它就会相应地处理它。在您的第一个示例中,您确定这些块实际执行了吗?
断言可能会导致异常,这可能会破坏您尝试执行的操作。如果你的承诺库支持它,你总是可以:
it("Should not do the work",function(done) {
axios.post("x/y",{ id:a2 })
.then(function(res) {
assert(false,"Should not do the work");
})
.catch(function(res) {
assert.equal(HttpStatus.CONFLICT,res.status);
})
.finally(done);
});
无论如何,确保应该这样做。
甚至更好:
it("Should not do the work",function() {
return axios.post("x/y",{ id:a2 })
.then(function(res) {
assert(false,"Should not do the work");
})
.catch(function(res) {
assert.equal(HttpStatus.CONFLICT,res.status);
})
});
注意对断言和在捕获中做断言。更好的计划可能是异步:
it("Should not do the work", async function() {
var res = await axios.post("x/y",{ id:a2 })
assert.equal(HttpStatus.CONFLICT,res.status);
});