【问题标题】:Why is this Angular directive not compiling inside of Jasmine unit test?为什么这个 Angular 指令没有在 Jasmine 单元测试中编译?
【发布时间】:2016-06-27 08:40:18
【问题描述】:

前提条件:我正在使用 Karma 对我的 Angular.js 应用模块运行 Jasmine 单元测试。

我的应用使用以下模式来公开模块(服务/指令/控制器):

simple.js

'use strict';

export default (angular) => {
    angular.module('simple', [])
        .directive('simple', [function() {
            return {
                restrict: 'E',
                replace: true,
                template: '<h1>COMPILED!</h1>'
            };
        }]);
};

上例对应的单元测试如下所示:

simple.test.js

import angular from 'angular';
import mocks from 'angular-mocks';
import simpleModule from './simple';

describe('simple', () => {
    var $compile,
        $rootScope;

    // Load the simple module, which contains the directive
    beforeEach(() => {
        let simpleComponent = new simpleModule(angular);
        // module('simple') raises a TypeError here
        // I have also tried angular.module('simple') which
        // I am pretty sure is incorrect.
    });

    // Store references to $rootScope and $compile
    // so they are available to all tests in this describe block
    beforeEach(inject((_$compile_, _$rootScope_) => {
        // The injector unwraps the underscores (_) from around the
        // parameter names when matching
        $compile = _$compile_;
        $rootScope = _$rootScope_;
    }));

    it('Replaces the element with the appropriate content', () => {
        // Compile a piece of HTML containing the directive
        var element = angular.element("<simple>not compiled</simple>");

        var compiledElement = $compile(element)($rootScope);

        // fire all the watches, so the scope expressions evaluate
        $rootScope.$digest();

        // Check that the compiled element contains the templated content
        expect(element.html()).toContain("COMPILED!");

    });

});

问题:在网络浏览器中使用 Karma 运行上述测试时,测试失败并且元素似乎没有被编译。

我错过了什么?

【问题讨论】:

  • 我遇到了同样的问题。
  • 你解决了这个问题吗?如果是这样,你能分享答案吗?

标签: javascript angularjs unit-testing webpack karma-jasmine


【解决方案1】:

我可以在您的代码中看到您在创建 $compile 之前缺少创建新的 $scope。你应该这样做:

it('Replaces the element with the appropriate content', () => {
    // Compile a piece of HTML containing the directive
    var scope = $rootScope.$new(); // create a new scope
    var element = angular.element("<simple>not compiled</simple>");
    var compiledElement = $compile(element)(scope);

    // fire all the watches, so the scope expressions evaluate
    scope.$digest();

    // Check that the compiled element contains the templated content
    expect(element.html()).toContain("COMPILED!");
});

【讨论】:

    【解决方案2】:

    我怀疑您没有正确导入指令。你试过了吗:

    beforeEach(module('simple'));
    

    您表示您尝试的替代版本不正确或者是我未见过的模式:

    beforeEach(() => {
        let simpleComponent = new simpleModule(angular);
    });
    
    module('simple');
    
    angular.module('simple');
    

    【讨论】:

      【解决方案3】:

      很明显,您在测试中使用的是 Javascript ES6。 Jasmine 目前只了解 ES5。

      尝试使用这个Jasmine ES6 Compiler,或者用 ES5 编写你的测试。

      注意:您的测试没有被编译但它们失败似乎是矛盾的。如果业力试图运行它们,它们只会失败。而且它们只有在编译后才能运行。你确定吗?

      【讨论】:

        猜你喜欢
        • 2015-01-18
        • 1970-01-01
        • 2014-04-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多