断言失败后的摩卡流控
Mocha flow control after assertion fails
我认为mocha会在断言失败后停止运行当前测试用例,像这样
it('test', function(done) {
a.should.equal(b);
//if a is not equal to be, won't go here
//do something
done();
}
我需要在断言失败后继续做一些事情,我尝试使用 try...catch,但是没有 "else" 用于 catch,所以如果我这样做
try {
a.should.equal(b)
} catch(e) {
console.log(e)
done(e)
} finally {
//do something
done()
}
这将调用 done() 两次,所以我必须添加一个标志,
var flag = true;
try {
a.should.equal(b)
} catch(e) {
console.log(e)
flag = false
done(e)
} finally {
//do something
if(flag)
done()
}
我觉得这太复杂了,所以我想知道是否有更简单的方法来做?
它被调用两次的原因是因为 catch
正在处理异常,但是 finally
块将 总是 执行,无论是否断言会引发错误。
使用 return
来处理您的场景 - 如果捕获到异常,它将 return 处理错误,否则它将跳过捕获并仅 return done()
.
try {
// test assertion
a.should.equal(b)
} catch(e) {
// catch the error and return it
console.log(e)
return done(e)
}
// just return normally
return done()
如果您需要在 if/else 场景中使用它,您可以使用变量来处理它。
var theError = false;
try {
// test assertion
a.should.equal(b)
} catch(e) {
// indicate we caught an error
console.log(e)
theError = e
}
if (theError) {
// there was an exception
done(theError)
} else {
// no exceptions!
done()
}
记住 finally 要么失败要么不失败。这就是它执行两次完成的原因。
You can do:
try {
a.should.equal(b)
} catch(e) {
console.log(e)
done(e)
}
done()
如果失败,您将通过捕获退出,否则您继续前进,return 正常。
after
挂钩在测试失败时仍会被调用,因此您可以将测试置于具有此类挂钩的上下文中:
describe('suite', function() {
after(function(done) {
// do something
done();
});
it('test', function(done) {
a.should.equal(b);
done();
}
});
测试运行器的工作是决定是否应该放弃或继续测试套件中的其他测试。
如果您希望测试继续 运行 从您的 mocha.opts
中删除 --bail
https://mochajs.org/api/mocha
我认为mocha会在断言失败后停止运行当前测试用例,像这样
it('test', function(done) {
a.should.equal(b);
//if a is not equal to be, won't go here
//do something
done();
}
我需要在断言失败后继续做一些事情,我尝试使用 try...catch,但是没有 "else" 用于 catch,所以如果我这样做
try {
a.should.equal(b)
} catch(e) {
console.log(e)
done(e)
} finally {
//do something
done()
}
这将调用 done() 两次,所以我必须添加一个标志,
var flag = true;
try {
a.should.equal(b)
} catch(e) {
console.log(e)
flag = false
done(e)
} finally {
//do something
if(flag)
done()
}
我觉得这太复杂了,所以我想知道是否有更简单的方法来做?
它被调用两次的原因是因为 catch
正在处理异常,但是 finally
块将 总是 执行,无论是否断言会引发错误。
使用 return
来处理您的场景 - 如果捕获到异常,它将 return 处理错误,否则它将跳过捕获并仅 return done()
.
try {
// test assertion
a.should.equal(b)
} catch(e) {
// catch the error and return it
console.log(e)
return done(e)
}
// just return normally
return done()
如果您需要在 if/else 场景中使用它,您可以使用变量来处理它。
var theError = false;
try {
// test assertion
a.should.equal(b)
} catch(e) {
// indicate we caught an error
console.log(e)
theError = e
}
if (theError) {
// there was an exception
done(theError)
} else {
// no exceptions!
done()
}
记住 finally 要么失败要么不失败。这就是它执行两次完成的原因。
You can do:
try {
a.should.equal(b)
} catch(e) {
console.log(e)
done(e)
}
done()
如果失败,您将通过捕获退出,否则您继续前进,return 正常。
after
挂钩在测试失败时仍会被调用,因此您可以将测试置于具有此类挂钩的上下文中:
describe('suite', function() {
after(function(done) {
// do something
done();
});
it('test', function(done) {
a.should.equal(b);
done();
}
});
测试运行器的工作是决定是否应该放弃或继续测试套件中的其他测试。
如果您希望测试继续 运行 从您的 mocha.opts
中删除 --bail
https://mochajs.org/api/mocha