我有這樣角JS單元測試控制器
(function(){
var app = angular.module('app', []);
app.directive('test', function(){
return {
restrict: 'E',
templateUrl: 'test.html',
controller: ['$scope', function ($scope) {
$scope.password = '';
$scope.grade = function() {
var size = $scope.password.length;
if (size > 8) {
$scope.strength = 'strong';
} else if (size > 3) {
$scope.strength = 'medium';
} else {
$scope.strength = 'weak';
}
}
}];
});
我寫一個單元測試到該控制器的控制器
describe('PasswordController', function() {
beforeEach(module('app'));
var $controller;
beforeEach(inject(function(_$controller_){
// The injector unwraps the underscores (_) from around the parameter names when matching
$controller = _$controller_;
}));
describe('$scope.grade', function() {
it('sets the strength to "strong" if the password length is >8 chars', function() {
var $scope = {};
var controller = $controller('$scope', { $scope: $scope });
$scope.password = 'longerthaneightchars';
$scope.grade();
expect($scope.strength).toEqual('strong');
});
});
});
我結束了得到一個錯誤,其 錯誤:[ng:areq]參數'$ scope'不是函數,未定義
我正在以正確的方式請求幫助
你的控制器有些問題,它是無效的JavaScript。你可以粘貼它嗎? –
(函數(){ VAR應用= angular.module( '應用',[]); app.directive( '試驗',函數(){ 返回{ 限制: 'E', templateUrl:「測試.html', controller:['$ scope',function($ scope){.........}],};});})(); – vino