未使用业力 spyOn 调用函数
function is not being called with a karma spyOn
我的控制器是:
window.map.controller('ScheduleHomeController', ['$scope', '$location', '$route', '$routeParams', 'cache', 'patientCache',
function($scope, $location, $route, $routeParams, cache, patientCache) {
cache.set('ui', 'headerMessage', '');
if(cache.get('patient', 'currentPatient').patientId !== $routeParams.patientId) {
cache.set('patient', 'currentPatient', patientCache.getPatient(parseInt($routeParams.patientId, 10)));
}
switch($route.current.originalPath) {
case '/patients/:patientId/schedule':
cache.set('ui', 'schedulePage', 'daily-schedule');
$scope.showScheduleNavbar = true;
break;
default:
$scope.showScheduleNavbar = false;
break;
}
}
]);
我的测试是:
fdescribe('ScheduleHomeController', function() {
var scope, cache, $route, patientCache, $routeParams;
beforeEach(function() {
module('mapApp');
return inject(function($injector) {
var $controller, $rootScope;
$rootScope = $injector.get('$rootScope');
$controller = $injector.get('$controller');
$route = $injector.get('$route');
cache = $injector.get('cache');
patientCache = $injector.get('patientCache');
$routeParams = $injector.get('$routeParams');
scope = $rootScope.$new()
$route.current = { originalPath: '/patients/:patientId/schedule' };
$controller('ScheduleHomeController', {
$scope: scope,
cache: cache,
patientCache: patientCache,
$route: $route,
$routeParams: $routeParams
});
return scope.$digest();
});
});
it('should set the ui.headerMessage to empty', function() {
spyOn(cache, 'set');
expect(cache.set).toHaveBeenCalledWith('ui', 'headerMessage', '');
});
});
所以我希望调用 cache.set
,但我得到的是 Expected spy set to have been called with [ 'ui', 'headerMessage', '' ] but it was never called.
我做错了什么?
您必须在函数执行之前监视 cache.set(在控制器初始化中)。所以把它放在 $contorller 之前:
...
spyOn(cache, 'set');
$controller('ScheduleHomeController', {
...
我的控制器是:
window.map.controller('ScheduleHomeController', ['$scope', '$location', '$route', '$routeParams', 'cache', 'patientCache',
function($scope, $location, $route, $routeParams, cache, patientCache) {
cache.set('ui', 'headerMessage', '');
if(cache.get('patient', 'currentPatient').patientId !== $routeParams.patientId) {
cache.set('patient', 'currentPatient', patientCache.getPatient(parseInt($routeParams.patientId, 10)));
}
switch($route.current.originalPath) {
case '/patients/:patientId/schedule':
cache.set('ui', 'schedulePage', 'daily-schedule');
$scope.showScheduleNavbar = true;
break;
default:
$scope.showScheduleNavbar = false;
break;
}
}
]);
我的测试是:
fdescribe('ScheduleHomeController', function() {
var scope, cache, $route, patientCache, $routeParams;
beforeEach(function() {
module('mapApp');
return inject(function($injector) {
var $controller, $rootScope;
$rootScope = $injector.get('$rootScope');
$controller = $injector.get('$controller');
$route = $injector.get('$route');
cache = $injector.get('cache');
patientCache = $injector.get('patientCache');
$routeParams = $injector.get('$routeParams');
scope = $rootScope.$new()
$route.current = { originalPath: '/patients/:patientId/schedule' };
$controller('ScheduleHomeController', {
$scope: scope,
cache: cache,
patientCache: patientCache,
$route: $route,
$routeParams: $routeParams
});
return scope.$digest();
});
});
it('should set the ui.headerMessage to empty', function() {
spyOn(cache, 'set');
expect(cache.set).toHaveBeenCalledWith('ui', 'headerMessage', '');
});
});
所以我希望调用 cache.set
,但我得到的是 Expected spy set to have been called with [ 'ui', 'headerMessage', '' ] but it was never called.
我做错了什么?
您必须在函数执行之前监视 cache.set(在控制器初始化中)。所以把它放在 $contorller 之前:
...
spyOn(cache, 'set');
$controller('ScheduleHomeController', {
...