【发布时间】:2014-08-04 19:45:11
【问题描述】:
我正在为 Jasmine 中的控制器编写测试;控制器依赖于我使用beforeEach(inject) 注入的服务。但是,当我尝试监视服务中的方法时,总是会收到以下错误:
spyOn could not find an object to spy upon for replacePod()
我的测试代码如下:
describe('Controller: ResultsController', function () {
'use strict';
var ctrl;
var ResultsService;
var RouteService;
var PodService;
var $scope;
var httpMock;
var log;
// load the controller's module
beforeEach(module('waApp'));·
// Initialize the controller and a mock scope
····
beforeEach(inject(function(
$rootScope,
$log,
$controller,
$httpBackend,
ResultsService,
RouteService,
PodService
) {
$scope = $rootScope.$new();
log = $log;
log.reset();
httpMock = $httpBackend;
ctrl = $controller('ResultsController', {
$scope: $scope,
ResultsService: ResultsService,
RouteService: RouteService,
PodService: PodService
});
}));
it('processRecalculate should call PodService.replacePod', function() {
$scope.getResults({input:'kitten'});
spyOn(PodService, 'replacePod');
expect(PodService.replacePod).toHaveBeenCalled();
});
这会产生上述错误。我的代码有问题,还是无法监视注入服务中的方法?
【问题讨论】:
标签: javascript angularjs jasmine