我是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.
}));
});
在测试代码中,注册控制器的最佳方法是什么?我需要创建一个模拟的吗?
由于
答案 0 :(得分:1)
如果控制器SomeController
位于同一模块application.directives
中,则您无需进行任何操作。在测试指令时,控制器将可用。
Here is the sample code in jsfiddle for similar case
如果控制器在不同的模块中。