3

我正在尝试对在段落、锚标记中具有变量的 html 模板进行单元测试,{{header.title || translate}}但是无论我尝试过哪些帖子,它似乎都不起作用。我得到了检索到的 HTML 模板,当它被编译时它仍然是一样的。在模板中我仍然看到{{user}}例如。似乎它们实际上都没有被编译。

当前模板输出:

<h1>{{header.title | translate}}</h1>
<h2>{{homeCtrl.name}}</h2>

预期输出:

<h1>Cake Blogger</h1>
<h2>Alexandria</h2>

测试套件:

(function() {
  'use strict';

  describe('home.tpl.html', function() {
    var scope, controller, createController, template, element, rootScope;

    beforeEach(module('Templates'));
    beforeEach(module('mainApp'));

    /**
      @name: home.tpl.html
      @description: Inject and set test related objects
      @param {Service} rootScope - Used to get the language
      @param {Compiler} $templateCache - Holding the compiled template
      @param {Injector} $compile - Compiles an HTML string or DOM
    */
    beforeEach(inject(
      function(_$controller_, _$rootScope_, $templateCache, $compile) {
        scope = _$rootScope_.$new();
        createController = function() {
          return _$controller_('homeCtrl', {
            '$scope': scope
         });
      };

      controller = createController();

      rootScope = _$rootScope_;
      template = $templateCache.get('home.tpl.html');
      element = $compile(template)(rootScope);

      // var ctrl = element.controller('homeCtrl');
      rootScope.$digest();
      console.log("home page", element);
  }));

  /**
    @name: Describe Block - home.tpl.html
    @description: Test cases related to home.tpl.html
  */
  describe('home.tpl.html tests', function() {
    fit('should have "Alexandria"', function() {
      expect(element.html()).toContain("Alexandria");
    });
  });
});
})();

业力档案:

files: ['list of files'],

port: 8080,

browsers: [
  'PhantomJS'
],

plugins: [
  'karma-phantomjs-launcher',
  'karma-jasmine',
  'karma-ng-html2js-preprocessor',
],

preprocessors: {
  'app/**/*.tpl.html': 'html2js'
},

ngHtml2JsPreprocessor: {
  'moduleName': 'Templates',
  'stripPrefix': 'app/'
}

包.json

{
  "name": "",
  "private": true,
  "devDependencies": {
    "autoprefixer-core": "^5.2.1",
    "grunt": "^0.4.5",
    "grunt-angular-templates": "^0.5.7",
    "grunt-concurrent": "^1.0.0",
    "grunt-contrib-clean": "^0.6.0",
    "grunt-contrib-compass": "^1.0.0",
    "grunt-contrib-concat": "^0.5.0",
    "grunt-contrib-connect": "^0.9.0",
    "grunt-contrib-copy": "^0.7.0",
    "grunt-contrib-cssmin": "^0.12.0",
    "grunt-contrib-htmlmin": "^0.4.0",
    "grunt-contrib-imagemin": "^1.0.0",
    "grunt-contrib-jshint": "^0.11.0",
    "grunt-contrib-uglify": "^0.7.0",
    "grunt-contrib-watch": "^0.6.1",
    "grunt-filerev": "^2.1.2",
    "grunt-google-cdn": "^0.4.3",
    "grunt-jscs": "^1.8.0",
    "grunt-karma": "*",
    "grunt-modernizr": "^1.0.2",
    "grunt-newer": "^1.1.0",
    "grunt-ng-annotate": "^0.9.2",
    "grunt-ng-constant": "^2.0.1",
    "grunt-postcss": "^0.5.5",
    "grunt-svgmin": "^2.0.0",
    "grunt-usemin": "^3.0.0",
    "grunt-wiredep": "^2.0.0",
    "jasmine-core": "^2.4.1",
    "jit-grunt": "^0.9.1",
    "jshint-stylish": "^1.0.0",
    "karma": "^0.13.22",
    "karma-coverage": "^0.5.5",
    "karma-fixture": "^0.2.6",
    "karma-jasmine": "*",
    "karma-json-fixtures-preprocessor": "0.0.6",
    "karma-json-preprocessor": "^0.3.3",
    "karma-junit-reporter": "^1.2.0",
    "karma-ng-html2js-preprocessor": "~0.1.0",
    "karma-phantomjs-launcher": "*",
    "phantomjs": "^2.1.7",
    "time-grunt": "^1.0.0"
  },
  "engines": {
    "node": ">=0.10.0"
  },
  "scripts": {
    "test": "grunt test"
  },
  "dependencies": {}
}

根据我从 post plunker example阅读过的帖子,例如 this plunker ,这应该可以正常工作。我在想控制器可能没有绑定到范围,因此在$digest运行模板时可能找不到控制器。

有用的信息:

  • 它说rootscope.$digest()我也尝试过scope.$digest(),我也尝试过使用$apply()
  • 我在用ngHtml2JsPreprocessor
  • 我在用TemplateCache
  • 我在用$compile

查看的链接:

模板返回,但模板中的角度永远不会被编译。总是看到{{homeCtrl.name}}而不是Alexandria

更新 1.1

我在想,也许是因为翻译{{header.title | translate}}不起作用,也许angular-translate(module pascalprecht.translate:) 实际上没有正常工作,然后导致控制器也绑定失败。将继续调查。

4

1 回答 1

1

注入过滤器后angular-translate| translate过滤器正常工作并在 $compile 后产生预期的翻译值。以这个 plunker 为例:

http://plnkr.co/edit/j8rbnMI067YntllwTcGi?p=preview

导致您的问题的一个可能原因是时间 - 假设您有很大的翻译数组 / json 并且需要一段时间才能加载,但 Jasmine 测试在完全加载和准备好之前已经开始和完成。然后 Jasmine 将在测试期间看到未翻译的字符串。


更新于 2017-06-30

经过几次实验,我可以确认任何异步 json 加载器,无论是.useStaticFilesLoader还是$.getJSON(),都不会在 jasmine 之前执行。请参阅http://plnkr.co/edit/nreUd52iqOVvwLPMNtJA?p=preview作为示例,静态加载器适用于页面视图,但单元测试失败。

一种可能的方法是.useStaticFilesLoader完全放弃。相反,我们可以使用grunt-replace在构建过程中注入翻译。咕噜任务示例:

// replace string with json
replace: {
  dist: {
    options: {
      patterns: [
        {
          match: /\"JSONEN\"/,
          replacement: '<%= grunt.file.read("app/resources/en.json") %>'
        }
      ]
    },
    files: [
      {expand: true, flatten: true, src: ['app/scripts/app.js'], dest: 'public/'}
    ]
  }
}
于 2017-06-29T02:38:32.527 回答