我有这样角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