为什么运行这个角度测试会抛出“无法识别的表达式”?

时间:2015-08-10 20:49:53

标签: angularjs unit-testing angularjs-directive

我的指示就是这个

angular.module('My.directives').
 directive('documentIcon',
   function(MyService) {
  return {
    restrict: 'A',
    replace: true,
    scope: {
      aDocument: '=',
      user: '='
    },
    templateUrl: '/partials/documentIcon.html',
    link: function(scope, elem, attrs) {
      scope.alignBottom = 'alignBottom' in attrs;

        //business stuff 
        }
      };
    }
  };
});

我的模板html就是这个

<span
</span>
是的......在我认为有空格或特殊字符后,我把它浇水了。

当我运行我的规范时,这是

describe('directives', function() {
    var elm, scope;

    beforeEach(module('My.directives'
      '/partials/documentIcon.html'));

    beforeEach(inject(function($rootScope) {
      scope = $rootScope;
    }));

    it('should have the correct isolate scope values', inject(function($compile) {
      scope.detailDocument = {
        id: 'xyz',
        isChangingActiveState: true,
        public: true,
        starred: true
      };

      scope.user = {
        id: "12435"
      };

      elm = angular.elment('<span document-icon a-document="detailDocument" align-bottom></span>');
          $compile(elm)(scope);

      scope.$apply();

      console.log(elm);
    }));
  });

我明白了

  Error: Syntax error, unrecognized expression: angular.module('/partials/documentIcon.html', []).run(function($templateCache) {
        $templateCache.put('/partials/documentIcon.html',
          '<span>\n' +
          '</span>');
      });
          at ......project/web/js/lib/jquery-1.11.0.min.js:2
          at ob (......project/web/js/lib/jquery-1.11.0.min.js:2)
          at xb (......project/web/js/lib/jquery-1.11.0.min.js:2)
          at db (......project/web/js/lib/jquery-1.11.0.min.js:2)
          at ......project/web/js/lib/jquery-1.11.0.min.js:2
          at ......project/web/js/lib/jquery-1.11.0.min.js:2
          at ......project/web/js/lib/jquery-1.11.0.min.js:2
          at removeComments (......bower_components/angular/angular.js:8352)
          at ......bower_components/angular/angular.js:7935
          at processQueue (......bower_components/angular/angular.js:13248)
          at ......bower_components/angular/angular.js:13264
          at ......bower_components/angular/angular.js:14466
          at ......bower_components/angular/angular.js:14282
          at ......bower_components/angular/angular.js:14571
          at /......test/helpers/angular-spec-helpers.js:28
          at invoke (......bower_components/angular/angular.js:4203)
          at workFn (......bower_components/angular-mocks/angular-mocks.js:2436)
          at ......bower_components/angular-mocks/angular-mocks.js:2408
          at /......test/helpers/angular-spec-helpers.js:29
          at /......test/unit/directives/documentIconSpec.js:31
      undefined

模板中没有任何特殊内容。 为什么我收到此错误?

1 个答案:

答案 0 :(得分:3)

我刚刚为自己修正了一个看起来像这样的错误,所以我想我发布了我在这里发现的错误,因为它是一个相对较新的问题。

您正在使用karma-ng-html2js-preprocessor软件包,发生的事情是,声明用于缓存模板的角度模块的代码将作为模板加载,而不仅仅是模板HTML。

我遇到这种情况的原因是我无意中也在同一组文件上运行另一个预处理器karma-html2js-preprocessor。我建议您在karma.conf.js

中查找类似的内容
preprocessors: {
  '**/*.html': 'html2js',
  // ... other preprocessors
  '**/*.html': 'ng-html2js'
},

您希望确保其中只有一个正在模板上运行。