我写了一个实习测试,它做了一些相互依赖的xhr调用(登录,获取数据)。所以,我已经嵌套了它们,但仍然希望能够在我的处理程序中使用chai
断言库。
我发现测试没有正确失败,它总是挂起,最后是实习生报告:
FAIL: main - MySuite - Make some async requests.. (10012ms)
CancelError: Timeout reached on main - MySuite - Make some async requests..
这是一段代码:assert(false, 'Oh no, something went wrong');
已执行。
从我在assert库中看到的内容中,它抛出了预期会在调用堆栈中被捕获的异常,但这种方法不适合异步请求处理程序的调用堆栈。 / p>
我可以在代码中使用assert()样式函数,还是我被迫拒绝this.async(timeout)
给我的原始dfd?
这个问题与Async test doesn't error on fail的不同之处在于他滥用了this.async()中的原始dfd。我试图不使用它,而是使用chai断言库的更高级别的abstration。
我的简化测试模块:
/*jshint dojo:true */
/*global console:true */
'use strict';
define([
'intern!tdd',
'intern/chai!assert',
'intern/dojo/request'
], function (test, assert, request) {
console.log('Test has started to run.');
var testTimeout = 10000;
test.suite('MySuite', function () {
test.test('Make some async requests..', function () {
var dfd = this.async(testTimeout);
var promise = request('http://dojotoolkit.org/js/dojo/1.8/release/dtk/dijit/themes/claro/claro.css')
.then(function (res) {
console.log('First request OK: ', res.length, ' chars.');
// Make a second request
request('http://dojotoolkit.org/css/print.css')
.then(function (res2) {
console.log('Second request OK: ', res2.length, ' chars.');
// Now pretend we hit an error
console.log('Faking an assert fail...');
assert(false, 'Oh no, something went wrong');
// We would have got here if it weren't for those pesky assertions
dfd.resolve('test passed');
}, function (err) {
// Record the error
console.log('Inner Error handler was hit: ', err);
//Error Callback
//Ensure no HTTP errors raised.
dfd.reject.bind(dfd);
});
},
function (err) {
// Record the error
console.log('Outer Error handler was hit: ', err);
//Error Callback
//Ensure no HTTP errors raised.
dfd.reject.bind(dfd);
});
});
});
});
intern.js:
// Learn more about configuring this file at <https://github.com/theintern/intern/wiki/Configuring-Intern>.
// These default settings work OK for most people. The options that *must* be changed below are the
// packages, suites, excludeInstrumentation, and (if you want functional tests) functionalSuites.
define([ 'intern/node_modules/dojo/has' ], function (has) {
has.add('dojo-has-api', true);
return {
// Configuration options for the module loader; any AMD configuration options supported by the specified AMD loader
// can be used here
loader: {
// Packages that should be registered with the loader in each testing environment
packages: [
'node',
{ name: 'testing', location: '.' }
]
},
// Non-functional test suite(s) to run in each browser
suites: [ 'testing' /* 'myPackage/tests/foo', 'myPackage/tests/bar' */ ]
}
});
shell输出:
neek@alyssa:~/src/WIN/testing$ node node_modules/.bin/intern-client config=intern suites=internpromises
Defaulting to "console" reporter
Test has started to run.
First request OK: 135540 chars.
Second request OK: 135540 chars.
Faking an assert fail...
FAIL: main - MySuite - Make some async requests.. (10009ms)
CancelError: Timeout reached on main - MySuite - Make some async requests..
at Error (<anonymous>)
答案 0 :(得分:2)
在异步测试中,如果只是希望在断言失败时测试失败,则需要使用dfd.rejectOnError(callback)
包装回调函数;如果要在断言失败时希望测试失败,则需要dfd.callback(callback)
当没有断言失败时成功。有关详细信息,请参阅文档的asynchronous testing部分。