我试图为下面的代码编写单元测试,但我无法实现它。
服务代码:
angular.module('ActivityApp').service('PersonService', [
'$http',
function ($http) {
var person = function () {
return $http({
method: 'GET',
url: '/apis/person'
});
};
return {
Person: person
}
}]);
控制器代码
angular.module('ActivityApp').controller('PersonController', [
'$scope',
'PersonService',
function ($scope, PersonService) {
'use strict';
$scope.person = PersonService.Person().then(function(res) { alert(res.data) });
$scope.save = function () {
PersonService.Person().then(function (res) { alert(res.data) });
};
}]);
单元测试代码:
describe("EDI controller", function () {
var $httpBackend, $rootScope, mycontroller, personService, $http, $httpBackend, $scope, deferred, $controller;
beforeEach(module('ediapp'));
beforeEach(function () {
filesAjaxService = jasmine.createSpyObj('personService', [
'Person'
]);
module(function ($provide) {
$provide.value('personService', personService);
});
});
beforeEach(inject(function (_$controller_, _$rootScope_, _personService_, _$http_, _$httpBackend_) {
// The injector unwraps the underscores (_) from around the parameter names when matching
$controller = _$controller_;
$scope = _$rootScope_.$new();
$rootScope = _$rootScope_;
personService = _personService_;
$http = _$http_;
$httpBackend = _$httpBackend_;
mycontroller = $controller("filesCtrl", {
$scope: $scope,
PersonService: personService
});
}));
it("Called", function () {
// expect(fileController).toBeDefined();
// expect(filesAjaxService.GetAllFiles).toHaveBeenCalled();
});
afterEach(function () {
// $httpBackend.verifyNoOutstandingExpectation();
// $httpBackend.verifyNoOutstandingRequest();
});
});
当我运行上述测试用例时,出现以下错误
TypeError: undefined is not an object (评估 'PersonService.Person().then(')
任何人都可以提供解决此问题的解决方案吗?