2015-10-14 155 views
0

我有這樣角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'不是函數,未定義

我正在以正確的方式請求幫助

+0

你的控制器有些問題,它是無效的JavaScript。你可以粘貼它嗎? –

+0

(函數(){ VAR應用= angular.module( '應用',[]); app.directive( '試驗',函數(){ 返回{ 限制: 'E', templateUrl:「測試.html', controller:['$ scope',function($ scope){.........}],};});})(); – vino

回答

-1

您無法通過執行$scope = {}來創建$scope。你的天賦改成這樣:

describe('PasswordController', function() { 
    beforeEach(module('app')); 

    var $controller, $rootScope; 

    beforeEach(inject(function (_$controller_, _$rootScope_) { 
      // The injector unwraps the underscores (_) from around the parameter names when matching 
      $controller = _$controller_; 
      $rootScope = _$rootScope_; 
     })); 

    describe('$scope.grade', function() { 
     it('sets the strength to "strong" if the password length is >8 chars', function() { 
      var $scope = $rootScope.$new(); 
      var controller = $controller('$scope', { 
        $scope : $scope 
       }); 
      $scope.password = 'longerthaneightchars'; 
      $scope.grade(); 
      expect($scope.strength).toEqual('strong'); 
     }); 
    }); 
}); 
+0

我最後得到相同的錯誤。 – vino

+0

控制器或單元測試中是否有任何錯誤? – vino

1

你的控制器定義爲您的指令定義的一部分,我不相信這些可以單元獨立指令themsleves的測試。

如果你想單元測試這個控制器,你應該使用angular的controller方法給它一個單獨的名字,然後在你的指令中按名稱使用它。然後,您可以使用角度模擬的$controller服務檢索控制器,與您現在的操作類似。最終的結果是這樣的:

app.controller('YourCtrl', ['$scope', function($scope) { ... }]); 
app.directive('test', function() { 
    return { 
      ... 
      controller: 'YourCtrl', 
      ... 
    }}); 

,並在測試

var controller = $controller('YourCtrl', { $scope: $scope }); 

這裏是一個的jsfiddle是puts it all together

+0

現在錯誤是YourCtrl不是函數 – vino

+0

這裏是一個jsFiddle你的代碼正在運行......也許你的問題是你的代碼中的一些其他錯字? http://jsfiddle.net/hzwx2tmd/1/ –

+0

http://jsfiddle.net/hzwx2tmd/3/我有類似這樣的控制器。你能幫忙做這個工作謝謝 – vino

0

這裏是我會測試該指令的控制器。 DEMO http://plnkr.co/edit/w9cJ6KDNDvemO8QT3tTN?p=preview

我不會導入控制器。我會編譯指令並測試指令的控制器。

describe('PasswordController', function() { 
    var $scope; 
    var element; 

    beforeEach(module('MyApp')); 

    beforeEach(
    inject(function($rootScope, $compile, $templateCache) { 
     // Imports test.html 
     var templateUrl = 'test.html'; 
     var req = new XMLHttpRequest(); 
     req.onload = function() { 
     $templateCache.put(templateUrl, this.responseText); 
     }; 
     req.open('get', templateUrl, false); 
     req.send(); 

     $scope = $rootScope.$new(); 

     element = '<test></test>'; 
     // Compile the directive 
     element = $compile(element)($scope); 

     // Call digest cycle 
     $scope.$apply(); 
    })); 

    describe('$scope.grade', function() { 
    it('sets the strength to "strong" if the password length is >8 chars', function() { 
     $scope.password = 'longerthaneightchars'; 
     $scope.grade(); 
     expect($scope.strength).toEqual('strong'); 
    }); 
    }); 
});