我的控制器是:
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.
我做错了什么?
答案 0 :(得分:2)
你必须在函数执行之前spyOn cache.set(在controllers init中)。所以把它放在$ contorller:
之前...
spyOn(cache, 'set');
$controller('ScheduleHomeController', {
...