我正在尝试测试AngularJS文件,但它无法调用另一个Controller中定义的方法.
在MyPage文件中:
angular.module('MyApplication').controller('MyPageCtrl', function ($scope, $rootScope) {
if($scope.pageChecker) {
$scope.doSomething();
}
}
Run Code Online (Sandbox Code Playgroud)
pageChecker()方法在MyApplication控制器的App.js中定义,并返回true或false.
为了完整起见:
$scope.pageChecker = function() {
if(cookieCheck) {
return true;
}
else {
return false;
}
};
Run Code Online (Sandbox Code Playgroud)
茉莉花文件是:
describe("Page check", function(){
beforeEach(angular.mock.module('MyApplication'));
beforeEach(angular.mock.inject(function ($rootScope, $controller) {
var $scope = $rootScope.$new();
$controller('MyPageCtrl', { $scope: $scope});
}));
it("should call $scope.doSomething", function(){
spyOn($scope, "doSomething");
$scope.doSomething();
expect($scope.doSomething).toHaveBeenCalled();
});
});
Run Code Online (Sandbox Code Playgroud)
我得到"TypeError:'undefined'不是一个函数(评估'$ scope.pageChecker()')
我想覆盖pageChecker方法以始终返回true.我该怎么做呢?
编辑:感谢Dskh,我有我的答案,还有我的另一个调整:
describe("Page check", function(){
var $scope; //declare here, else it is a local variable inside the beforeEach
beforeEach(angular.mock.module('MyApplication'));
beforeEach(angular.mock.inject(function ($rootScope, …Run Code Online (Sandbox Code Playgroud)