【问题标题】:When unit testing a directive how to pass its controller对指令进行单元测试时如何通过其控制器
【发布时间】:2017-03-14 15:34:54
【问题描述】:

我是 AngularJS 及其单元测试的新手。目前我正在尝试在模板文件中使用其控制器测试指令:

这是模板:

<div ng-controller = "SomeController" >
<select ng-model="data.selectedOption" 
        ng-options="data.code for data in data.availableOptions track by data.code">
</select>
</div>

是js文件:

angular.module('application.directives')
  .directive('selector', function () {
    return {
      restrict: 'E',
      templateUrl: 'templates.html'
     };
  });

现在我有一个单元测试,例如:

describe('selector', function () {
  beforeEach(module('application.directives','application.templates'));

  it('render the correct number of items', inject(function ($rootScope, $compile) {
    var element = $compile('<selector></selector>')($rootScope);
    $rootScope.$digest();
    //I'd like to register the selector's controller here.
  }));
});

在测试代码中,注册控制器的最佳方式是什么?我需要创建一个模拟的吗?

谢谢

【问题讨论】:

    标签: angularjs unit-testing angularjs-directive controller


    【解决方案1】:

    如果控制器SomeController在同一个模块application.directives中,你不需要太多。控制器将在测试指令时可用。

    Here is the sample code in jsfiddle for similar case

    如果控制器在不同的模块中。

    Here is the sample code in jsfiddle for this case

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-11
      • 2012-04-24
      • 2018-09-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多