2016-03-03 105 views
0

我一直在試圖創建一個樣本單元測試,但它不斷給我一個錯誤「參數'CalcController'不是一個函數,得到未定義」。茉莉花,'控制器'不是一個函數,得到undefined

我已經看到所有問題與相同的標題,並嘗試了他們的解決方案在這裏在計算器中,但似乎他們不適合我。

calc.controller.js

(function() { 
    'use strict'; 

    angular 
    .module('app.calc') 
    .controller('CalcController', CalcController); 

    /** @ngInject */ 
    function CalcController($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'; 
     } 
    } 
    } 
}); 

calc.module.js

(function() { 
    'use strict'; 

    angular 
    .module('app.calc', []) 
    .config(config); 

    /** @ngInject */ 
    function config() { 
    } 
})(); 

calc.specs.js

describe('CalcController', function() { 
    beforeEach(module('fuse')); 
    beforeEach(module('app.calc')); 

    var $controller; 

    beforeEach(inject(function(_$controller_){ 
    $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('CalcController', { $scope: $scope }); 
     $scope.password = 'longerthaneightchars'; 
     $scope.grade(); 
     expect($scope.strength).toEqual('strong'); 
    }); 
    }); 

    describe('test', function() { 
    it('should work', function() { 
     expect(true).toBe(true); 
    }); 
    }); 
}); 

如果您注意到示例函數是針對密碼輸入編寫的,這是因爲我從同一項目上的工作測試腳本複製了代碼。但顯然它不以同樣的方式工作。

乾杯

回答

1

自調用函數(function(){...}); calc.controller.js缺少()在這就是爲什麼它找不到控制器結束。

+0

請舉例嗎? – dopatraman

相關問題