0
以下のコードの単体テストを作成しようとしましたが、実現できません。ajaxサービスでAngular JSコントローラの単体テストを実装する方法は?
サービスコード:
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 (evaluating 'PersonService.Person().then(')
誰でもこの問題を解決するソリューションを提供できますか?
@georgeawgは、編集のためにありがとうございます:) – Maher