1

現在、promise を使用するカスタム ディレクティブをテストしています。

ディレクティブのコード セグメントの最初の部分の構造は次のとおりです。

angular.module('app').directive('simpleMock', [

'$q',
function ($q) {
    'use strict';

    return {
        link: function ($scope) {
            $scope.$watch('acc.mockPromise', function (promise) {
                var sF;

                $scope.sF = sF = {};

                promise.then(function (res) {
                    sF.res = res;

                    return $q.when(res.fetchData());
                }).then(function (data) {
                    sF.data = data;

                    return $q.when(sF.res.fetchLinks('create'));
                }).then(function (links) {
                    if (!links.length) {
                        return $q.when(sF.res.fetchLinks('edit'));
                    }

                    return links;
                })....
    ...............

私が書いたジャスミンのテスト仕様は、次のような構造になっています。

beforeEach(module('app'));
beforeEach(inject(function ($rootScope, $compile) {
    when = modulejs.require('when');
    sF = {};
    res = jasmine.createSpyObj('res', ['fetchData', 'fetchLinks']);
    sF.res = res;
    res.fetchData.andReturn(when([{
        href: 'http://promise.angularjs.com/',
        name: 'undefined',
        rel: 'rel',
        title: "Fake link"
    }]));
    res.fetchLinks.andReturn(when([{
        href: 'http://promise.angularjs.com/',
        name: 'undefined',
        rel: 'rel',
        title: "Fake link"
    }]));
    compile = function (acc, res) {
        var $childScope, $parentScope, element;

        element = angular.element('<div simple-mock></div>');
        $parentScope = $rootScope.$new();
        $parentScope.acc = acc;
        acc.mockPromise = res;
        $childScope = $parentScope.$new();
        $compile(element)($childScope);
        $parentScope.$digest();

        return $childScope;
    };
    getRootScope = function () {
        return $rootScope;
    }        
}));
it('calls fetchData', function () {
    var $scope, acc, $rootScope;

    acc = {};
    $scope = compile(acc, when(res));
    $rootScope = getRootScope();
    $scope.$apply();
    $rootScope.$apply();
    $rootScope.$apply();
    $rootScope.$apply();
    $scope.acc.mockPromise.then(function () {
        expect($scope.sF.res.fetchData).toHaveBeenCalled();
    });

このテストの実行中、ディレクティブ コードのこのセクションに制御が入りません。

.then(function (data) {

                    sF.data = data;

                    return $q.when(sF.res.fetchLinks('create'));

[ただし、コントロールはコードの前の行まで入ります。]

この問題の考えられる理由は、promise が解決されていないことです。また、エラーや例外はスローされません。仕様の「when」プロミスを解決するための提案 - コントロールがディレクティブの次のセクションに入るようにします。

                }).then(function (data) {
                    sF.data = data;

                    return $q.when(sF.res.fetchLinks('create'));
                }).then(function (links) {
                    if (!links.length) {
                        return $q.when(sF.res.fetchLinks('edit'));
                    }

                    return links;
                }) 
4

0 に答える 0