编译指令在 AngularJS 测试中失败 - karma jasmine

Compile directive failing in AngularJS testing - karma jasmine

我将 Karma 与 Jasmine Framework 结合使用来测试我的 AngularJS v1 应用程序。我已经设置了 ng-html2js 插件来将 partials 转换为模块。我可以使用 $templateCache.

访问部分

当我尝试编译该指令时,我什么也没得到。 url 的模板是使用 templateUrl.

指定的

我的项目结构是这样的。

project
|--index.php
|--karma.conf.js
|--assets
   |--app
      |--controllers
      |--directives
         |--my-directive.js
      |--partials
         |--my_directive.html
      |--tests
         |--directive-test.js

karma.conf.js

...
preprocessors: {
        "assets/app/partials/**/*.html": ['ng-html2js']
},
files: [
    ...
    'assets/app/controllers/**/*.js',
    'assets/app/directives/**/*.js',
    'assets/app/partials/**/*.html',
    'assets/app/tests/**/*.js',
],
ngHtml2JsPreprocessor: {
        moduleName: 'my.partials'
},
...

指令-test.js

describe('Test My Directive', function () {

    var $compile, $rootScope;

    beforeEach(function () {
        module('myapp');
        module('my.partials');

        inject(function (_$compile_, _$rootScope_, _$templateCache_) {
            $compile = _$compile_;
            $rootScope = _$rootScope_;
            $templateCache = _$templateCache_;

            // This prints the template to the console
            console.log($templateCache.get('assets/app/partials/my_directive.html'));

        });
    });

    it('check if directive compiles', function () {

        var scope = $rootScope.$new();
        var template = $compile('<my-directive></my-directive>')(scope);
        scope.$digest();

        var templateAsHtml = template.html();

        // This doesn't print the template to the console
        console.log('templateAsHtml', templateAsHtml);

        expect(templateAsHtml).toContain("Lorem ipsum");

    });
});

我的-directive.js

myapp.directive('myDirective', function() {
    return {
        replace:true,
        restrict:'E',
        templateUrl: 'assets/app/partials/my_directive.html',
        link: function (scope, element, attrs) {
            console.log('my directive');
        }
    }
});

assets/app/partials/directives/my_directive.html

<div><h2>Test Directive</h2><p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam et enim non sem bibendum maximus. Nunc at urna sit amet.</p></div>

如果我没记错的话,templateUrl 总是 一个异步请求,即使指定的 url 已经在 $templateCache 中。在您的指令定义中试试这个:

template: function($templateCache) {
    return $templateCache.get('assets/app/partials/my_directive.html');
}

我用这种方法解决了 - 感谢@Aaron Pool 的提示。在指令中使用 templateUrl 进行异步请求/AJAX 调用以获取模板。 $http AJAX 调用可以使用 $httpBackend 拦截。捕获 AJAX 调用并使用 $templateCache 中的模板进行响应。这是修改后的代码,假设 $httpBackend 被注入到 beforeEach() 块中。

it('check if directive compiles', function () {

    var scope = $rootScope.$new();

    // Intercept the AJAX to fetch template
    $httpBackend.whenGET('assets/app/partials/directives/my_directive.html')
    .respond($templateCache.get('assets/app/partials/directives/my_directive.html'));

    var template = $compile('<my-directive></my-directive>')(scope);

    $httpBackend.flush();

    scope.$digest();

    var templateAsHtml = template.html();

    // Now it prints the template to the console
    console.log('templateAsHtml', templateAsHtml);

    expect(templateAsHtml).toContain("Lorem ipsum");

});