2

karma-ng-html2js-preprocessor または $httpBackend を使用したくありません。動的に作成した templateCache モジュールがあります。

app.js

angular.module('myApp', ['ngRoute', 'ui.bootstrap', 'ui.router', 'appTemplates']);

templates.js

(function(){

'use strict';

angular.module('appTemplates', []).run(['$templateCache',
    function($templateCache) {
        $templateCache.put('js/Templates/datetimepicker.html', '<div>My html here</div>');
    }
]);
})();

そしてディレクティブ

datetimepicker.js

angular.module('myApp').directive('datetimepicker',
    function () {
        return {
            restrict: 'A',
            replace: false,
            templateUrl: 'js/Templates/datetimepicker.html'
        };
    }
);

問題は、ディレクティブをコンパイルするときに、私のテストが templateCache を使用したくないように見えることです。

test.js

(function () {

"use strict";

describe("Datetimepicker directive tests", function () {

    var scope, templateCache, element;

    // load the directive's module
    beforeEach(module('myApp'));

    beforeEach(inject(function ($rootScope, $templateCache) {
        scope = $rootScope;
        templateCache = $templateCache;


    }));

    it('should exist', inject(function ($compile) {

       //this console.log prints out the correct HTML from cache
  //console.log(templateCache.get('js/Templates/datetimepicker.html'));

        element = angular.element('<div data-datetimepicker></div>');
        element = $compile(element)(scope);
        // this logs the element
        console.log(element);
        //this $digest call throws the error
        scope.$digest();

        console.log(element);

        expect(element.html()).toContain('div');
    }));
});
})();

私は得る:

Error: Unexpected request: GET template/datepicker/datepicker.html

テストを実行すると、コンソールの $httpBackend でこれ以上のリクエストは期待されません。

どんな助けでも感謝します。ありがとう

4

3 に答える 3

0

手がかりはエラーにありました。datetimepicker ディレクティブには、 datepickerという Angular-UI ディレクティブが含まれています。これがエラーの原因です。私のディレクティブは単体テスト可能ではないと思います。このディレクティブの E2E テストに焦点を当てます。したがって、テンプレートには実際にはここに投稿したよりも多くの HTML が含まれているため、この質問は誤解を招くものです。とにかくありがとう、SO!願わくば、この回答が同じエラーが発生している人々に役立つことを願っています!

于 2015-09-09T08:18:15.290 に答える