Javascript 异步内部测试的异步xhr处理程序中的断言(false)不';不要考试不及格

Javascript 异步内部测试的异步xhr处理程序中的断言(false)不';不要考试不及格,javascript,asynchronous,dojo,intern,chai,Javascript,Asynchronous,Dojo,Intern,Chai,我编写了一个实习生测试,它执行一些相互依赖的xhr调用(登录、获取数据)。因此,我嵌套了它们,但仍然希望能够在处理程序中使用chai断言库 我发现测试没有完全失败,它总是挂起,最后实习生报告: FAIL: main - MySuite - Make some async requests.. (10012ms) CancelError: Timeout reached on main - MySuite - Make some async requests.. 这是depite有一行代码:as

我编写了一个实习生测试,它执行一些相互依赖的xhr调用(登录、获取数据)。因此,我嵌套了它们,但仍然希望能够在处理程序中使用
chai
断言库

我发现测试没有完全失败,它总是挂起,最后实习生报告:

FAIL: main - MySuite - Make some async requests.. (10012ms)
CancelError: Timeout reached on main - MySuite - Make some async requests..
这是depite有一行代码:
assert(false,'哦,不,出了问题')

从我在assert库中看到的情况来看,它抛出异常,这些异常预计会在调用堆栈的更高层被捕获,但这种方法不适用于异步请求处理程序的调用堆栈

我可以在代码中使用assert()样式的函数吗?还是我必须拒绝
this.async(timeout)
提供给我的原始dfd

这个问题与其他问题不同,因为他滥用了这个.async()中的原始dfd。。我试着不使用它,而是使用图书馆的更高层次的abstrations

我的简化测试模块:

/*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);
            });
        });
    });

});
实习医生:

// 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' */ ]

    }
});
//了解有关在上配置此文件的详细信息。
//这些默认设置对大多数人都适用。以下*必须*更改的选项为
//软件包、套件、排除仪器和(如果需要功能测试)功能套件。
定义(['intern/node_modules/dojo/has'],函数(has){
add('dojo-has-api',true);
返回{
//模块加载器的配置选项;指定AMD加载器支持的任何AMD配置选项
//可以在这里使用
加载器:{
//应在每个测试环境中向加载程序注册的包
套餐:[
“节点”,
{name:'测试',位置:'.'}
]
},
//要在每个浏览器中运行的非功能测试套件
套件:['testing'/*'myPackage/tests/foo','myPackage/tests/bar'*/]
}
});
外壳输出:

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>)
neek@alyssa:~/src/WIN/testing$node node_modules/.bin/intern client config=intern suites=intern
默认为“控制台”报告程序
测试已开始运行。
第一个请求正常:135540个字符。
第二个请求正常:135540个字符。
假装断言失败。。。
失败:main-MySuite-发出一些异步请求。。(10009ms)
CancelError:main-MySuite-发出一些异步请求时达到超时。。
错误()

在异步测试中,如果您只是希望在断言失败时测试失败,则需要使用
dfd.rejectOnError(callback)
包装回调函数;如果您希望在断言失败时测试失败,或在没有断言失败时测试成功,则需要使用
dfd.callback(callback)
包装回调函数。有关更多信息,请参阅文档部分。

好吧,看来在执行断言之前已经达到了超时时间……谢谢Bergi。。您的意思是在断言(失败)之前的console.log语句和正在执行的断言之间?:)我相信intern正在等待
this.async()
返回的dfd被解析或拒绝。这是有道理的,但我相信,
intern
可以与
chai
一起“很好地工作”,从天真的角度来看,这意味着你可以在任何地方使用
chai
断言,它们都会起作用。在dojo/请求处理程序中使用它们似乎不是一个安全的地方。我只是在这里寻找最佳实践。谢谢。我已经读过了,但有点呆滞,因为它没有提到如何处理普通的request()错误处理程序,我也没有把2和2放在一起,将它关于错误的讨论与断言库联系起来。使用
dfd.RejectionError
似乎解决了我的问题。我想在测试失败时在数据库中记录一条记录。当尝试使用
测试时。在每个(处理程序)
之后,似乎都通过了测试的“结果”,在断言失败的情况下,这是未定义的,对吗?(这就是我在节点检查器调试会话中看到的,该会话在
afterEach()中被中断。