0

相互に依存するいくつかの 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 ライブラリ内で見たところ、コール スタックの上位でキャッチされると予想される例外がスローされますが、このアプローチは非同期リクエスト ハンドラのコール スタックには適していません。

コードのこの時点で assert() スタイルの関数を使用できますか? または、によって提供された元の dfd を拒否する必要がありthis.async(timeout)ますか?

この質問は、彼が this.async() から元の dfd を悪用していたという点で、 Async test doesn't error on failとは異なります。

私の簡易テスト モジュール:

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

    }
});

シェル出力:

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>)
4

1 に答える 1

2

非同期テストでは、アサーションが失敗したときにテストを失敗させたい場合、またはアサーションが失敗した場合にテストを失敗させたいdfd.rejectOnError(callback)場合、またはアサーションが失敗しないときにdfd.callback(callback)テストを成功させたい場合は、 を使用してコールバック関数をラップする必要があります。詳細については、ドキュメントの非同期テストの部分を参照してください。

于 2014-04-13T18:02:59.463 に答える