我正在尝试测试一段异步代码,但不幸的是,我得到了一个晦涩难懂的错误代码,而且我似乎找不出问题是什么。该测试在浏览器中运行良好,但在phantomjs中运行会导致:
Uncaught Script error. (:0)这个测试被写成一个requirejs模块,并且依赖于另一个模块。就像我说的,这在浏览器中工作得很好,当不执行异步测试时,在phantomjs中也工作得很好。我使用的是phantomjs 1.9.12和mocha-phantomjs 3.4.1。
define([ "csl" ], function( csl )
{
describe( "CSL", function()
{
it( "isLoggedIn", function( testCompleted )
{
csl.isLoggedIn().then( function( partner )
{
chai.expect( partner ).to.be.a( "object" );
testCompleted();
} )
.fail( function( error )
{
testCompleted( error );
} );
} );
} );
} );发布于 2015-05-03 11:49:34
这就是当异步函数中存在异常时,chai.expect会抛出AssertionError时mocha会产生的错误。
您可以在浏览器中通过简单的超时重现相同的错误:
it("should fail async", function(done) {
window.setTimeout(function() {
"".should.not.equal("");
done();
})
})要修复它,您需要通过回调报告错误,而不是异常:
it("should fail async with good error", function(done) {
window.setTimeout(function() {
if ("" == "") return done(new Error("Async error message"));
done();
})
})
it("should fail async with exception", function(done) {
window.setTimeout(function() {
try {
"".should.not.equal("");
}
catch (e) {
return done(e);
}
done();
})
})问题不在于幻影本身(除了导致测试失败的原因之外),而是测试运行器和幻影之间的连接使得一切都是异步的,从而触发了mocha bug。
https://stackoverflow.com/questions/26882155
复制相似问题