mik*_*ana 5 javascript unit-testing mocha.js
我正在运行测试,发现:
18 passing (150ms)
1 pending
Run Code Online (Sandbox Code Playgroud)
我以前没看过 先前的测试通过或失败。超时导致故障。我可以看到哪个测试失败了,因为它也是蓝色的。但是它有一个超时。这是一个简化的版本:
test(`Errors when bad thing happens`), function(){
try {
var actual = doThing(option)
} catch (err) {
assert(err.message.includes('invalid'))
}
throw new Error(`Expected an error and didn't get one!`)
}
Run Code Online (Sandbox Code Playgroud)
谢谢!
当我遇到这个问题时,挂起的错误是当我用跳过定义描述测试时,忘记删除它,如下所示:
describe.skip('padding test', function () {
it('good test', function () {
expect(true).to.equal(true);
})
});
Run Code Online (Sandbox Code Playgroud)
并运行它,我得到了输出
Pending test 'good test'
Run Code Online (Sandbox Code Playgroud)
当我在描述测试中删除跳过标志时,它再次工作..
当您无意中it提前关闭测试方法时,Mocha最终会将测试显示为“待处理” ,例如:
// Incorrect -- arguments of the it method are closed early
it('tests some functionality'), () => {
// Test code goes here...
};
Run Code Online (Sandbox Code Playgroud)
该it方法的参数应包括测试功能定义,如:
// Correct
it('tests some functionality', () => {
// Test code goes here...
});
Run Code Online (Sandbox Code Playgroud)
测试有一个回调(即,一个实际的函数,未完成),但重构代码解决了问题。问题是预期错误的代码应该如何运行:
test('Errors when bad thing happens', function() {
var gotExpectedError = false;
try {
var actual = doThing(option)
} catch (err) {
if ( err.message.includes('Invalid') ) {
gotExpectedError = true
}
}
if ( ! gotExpectedError ) {
throw new Error(`Expected an error and didn't get one!`)
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4551 次 |
| 最近记录: |