Domanda

Ho scritto un test stagista che fa alcune chiamate XHR (login, recupera dati) che dipendono l'uno dall'altro. Quindi, li ho annidati, ma spero ancora di poter utilizzare la Biblioteca di asserzione chai all'interno dei miei gestori.

Trovo che il test non è riuscito correttamente, si blocca sempre e infine riferisce:

FAIL: main - MySuite - Make some async requests.. (10012ms)
CancelError: Timeout reached on main - MySuite - Make some async requests..
.

Questo è il deposito con una linea di codice: assert(false, 'Oh no, something went wrong'); che viene eseguita.

Da quello che ho visto all'interno della Biblioteca Assert lancia eccezioni che dovrebbero essere catturate più in alto nella pila di chiamata, ma questo approccio non è adatto per la pila di chiamata di un gestore di richiesta ASYNC.

Posso utilizzare le funzioni di stile Assert () a questo punto del codice, o sono costretto a rifiutare il DFD originale dato da this.async(timeout)?

Questa domanda è diversa da ASYNC Test non errori di errore In che modo stava male ausualizzare il DFD originale da questo. Async () .. .. Sto cercando di non usarlo, e invece usare le astrimenti di livello superiore della Biblioteca Assertion Chai.

Il mio modulo di test semplificato:

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

    }
});
.

Uscita della 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>)
.

È stato utile?

Soluzione

In test asincrono, è necessario avvolgere le funzioni di callback utilizzando dfd.rejectOnError(callback) se si desidera semplicemente che il test non riesca a fallire quando un'asserzione fallisce o dfd.callback(callback) se si desidera che il test non riesca se non riesce quando nessuna asserzione fallisce.Vedi il test asincroni porzione della documentazione perMaggiori informazioni.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top