function foo(param){alert(" foo called"); }和$ scope.foo = function(param){alert(" foo");};

时间:2014-09-16 06:16:00

标签: javascript angularjs karma-jasmine use-strict unit-testing

  

我认为我和AngularJS有类似的问题。   我正在修复以前的错误消息(我无法调用   控制器功能来自测试描述块)并获得新的   错误。

     

错误:[ng:areq] Argument' fooController'不是一个功能,未定义

     

我已阅读其他帖子,但仍无法纠正。

     

所以我的控制器就像..

'use strict';

var app = angular.module('MyApp', ['MyAppControllers']);
var appControllers = angular.module('MyAppControllers', []);

appControllers.controller('fooController', ['$scope', function ($scope) {
function foo(param) {
    alert("foo called");
}
}]);
     

我的controllerspec是..

'use strict';

describe('fooController', function () {
    var $scope, $controller;

    beforeEach(inject(function ($rootScope, $controller) {
        $scope = $rootScope.$new();
        ctrl = $controller('fooController', { $scope: $scope });
    }));

    it("should write foo called", function () {
        $scope.foo();
   });
});
     

为什么一直说fooController不是函数?

     

谢谢大家。

旧帖子已编辑。请你阅读底部帖子(我的新问题?:))

2 个答案:

答案 0 :(得分:0)

更改控制器

appControllers.controller('fooController', ['$scope', function ($scope) {

//add your function within scope
$scope.foo = function(){
 alert("foo called");
}
}]);

答案 1 :(得分:0)

好。我很蠢。我没有在描述之后提出。

所以我现在用以下方法解决了问题:

'use strict';

describe('fooController', function () {
    **beforeEach(module('MyApp'))**;
    var $scope, ctrl;

    beforeEach(inject(function ($rootScope, $controller) {
        $scope = $rootScope.$new();
        ctrl = $controller('fooController', { $scope: $scope });
    }));

    it("should write foo called", function () {
        $scope.foo("aa");
    });
});

和控制器..

'use strict';

var app = angular.module('MyApp', ['MyAppControllers']);

var appControllers = angular.module('MyAppControllers', []);

appControllers.controller('fooController', ['$scope', function ($scope) {
    function foo(param) {
        alert("foo called");
    }
}]);

如果可以,我想问一下我原来的问题。

问题是无法从controllerSpec的测试块中看到函数foo TypeError:undefined不是函数

我看到一篇文章称函数是私有的,应该将函数转换为l

$scope.foo = function(param){alert("foo");};

但是'使用严格的'禁止转动功能如上所述。

我想知道其他人是如何解决这个问题的?

再次感谢你。

相关问题