0
我想測試一個角度指令。我以前做過這個沒有任何問題,但今天我陷入了困境。肯定是一個愚蠢的錯誤。指令單元測試控制器返回空對象
var element, scope, controller;
beforeEach(function() {
angular.mock.module('templates');
angular.mock.module('myApp.ui.apps.myMod');
angular.mock.module('myApp.ui.apps.myMod.dropdown-parameter');
angular.mock.inject(function($compile, $rootScope, $templateCache, $controller){
scope = $rootScope;
element = angular.element('<dropdown-parameter name-value-list="nameValueList" selected-option="selectedOption"></dropdown-parameter>');
scope.nameValueList = [];
var firstEnumValue = {
"Name":"TestMock",
"Value":"TestMock1Value"
};
var secondEnumValue = {
"Name":"TestMock",
"Value": "TestMock2Value"
};
scope.nameValueList.push(firstEnumValue);
scope.nameValueList.push(secondEnumValue);
scope.selectedOption={};
$compile(element)(scope);
scope.$digest();
controller = $controller('dropdownParameterController' ,{$scope: scope});
});
});
fit('Given an instantiated controller for dropdown parameter component when html is rendered then default value is the first one in the nameValueList', function(){
console.log(controller); // HERE RETURNS {}
expect(controller.selectedOption).toBe(controller.nameValueList[0].Value);
});
的問題是,控制器總是等於一個空對象
的console.log(控制器)=====> {} 我做錯了嗎?
這裏是我的控制器& &指令代碼。
var angular = require('angular');
module.exports = angular.module('myApp.ui.apps.myMod.dropdown-parameter', [])
.controller('dropdownParameterController', ['$scope', function($scope){
var self = this;
if(self.nameValueList)
{
self.selectedOption = self.nameValueList[0].Value;
}
}])
.directive('dropdownParameter', function(){
return {
restrict: 'E',
scope: true,
bindToController: {
nameValueList:'=',
selectedOption:'='
},
templateUrl: 'app/ui/apps/diagnostics/dropdown-parameter/dropdown-parameter.html',
controllerAs: 'dropdownParameterCtrl',
controller: 'dropdownParameterController'
};
});