我编写了一个实习生测试,它执行一些相互依赖的 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()风格的函数吗,或者我是否被迫拒绝给我的原始dfd this.async(timeout)?

这个问题不同于 异步测试失败时不会出错 因为他滥用了 this.async() 中的原始 dfd ..我试图不使用它,而是使用 chai 断言库的更高级别的抽象。

我的简化测试模块:

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

});

实习生.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' */ ]

    }
});

外壳输出:

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>)
有帮助吗?

解决方案

在异步测试中,您需要使用以下方法包装回调函数 dfd.rejectOnError(callback) 如果您只想在断言失败时测试失败,或者 dfd.callback(callback) 如果您希望测试在断言失败时失败或在没有断言失败时成功。请参阅 异步测试 文档的一部分以获取更多信息。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top