التأكيد (خطأ) داخل معالج xhr غير المتزامن لاختبار المتدرب غير المتزامن لا يفشل في الاختبار

StackOverflow https://stackoverflow.com//questions/23040070

سؤال

لقد قمت بكتابة اختبار متدرب يقوم ببعض مكالمات 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'); الذي يتم تنفيذه.

مما رأيته داخل مكتبة التأكيد، فإنها تطرح استثناءات من المتوقع أن يتم التقاطها في أعلى مكدس الاستدعاءات، ولكن هذا الأسلوب غير مناسب لمكدس الاستدعاءات لمعالج الطلب غير المتزامن.

هل يمكنني استخدام وظائف نمط تأكيد () في هذه المرحلة من التعليمات البرمجية، أم أنني مجبر على رفض dfd الأصلي الذي قدمه لي this.async(timeout)?

هذا السؤال يختلف عن لا يخطئ اختبار المزامنة عند الفشل من حيث أنه كان يسيء استخدام ملف dfd الأصلي من this.async() ..أحاول عدم استخدام ذلك، وبدلاً من ذلك استخدم تجريدات المستوى الأعلى لمكتبة تأكيد تشاي.

وحدة الاختبار المبسطة الخاصة بي:

/*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>)
هل كانت مفيدة؟

المحلول

في الاختبارات غير المتزامنة، تحتاج إلى تغليف وظائف رد الاتصال باستخدام dfd.rejectOnError(callback) إذا كنت تريد فقط أن يفشل الاختبار عند فشل التأكيد، أو dfd.callback(callback) إذا كنت تريد أن يفشل الاختبار في حالة فشل التأكيد أو ينجح في حالة عدم فشل أي تأكيد.انظر اختبار غير متزامن جزء من الوثائق لمزيد من المعلومات.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top