мне нужно проверить две вещи здесь:жасмин модульного тестирования контроллера
- Длина ответа. указали макетные данные как [{name: 'John', id: 1}, {name: 'Josh', id: 2}], поэтому длина ответа должна быть равна 2. он не выполняет этот тест, всегда получая длину как 1.
- Данные ответа должны быть равны. то есть. ожидать (IndexSummaryService.getIndexSummaryQueues) .toEqual ([{name: 'John', id: 1}, {name: 'Josh', id: 2}]);
Тест терпит неудачу с Message: Ожидаемая функция равного [объекта ({имя: 'Джон', ID: 1}), объект ({имя: 'Josh', ID: 2})]
Моя служба бит отличается от другой, которая принимает api как параметр, который является URL-адресом.
Просьба предложить, как сделать этот модульный тест работоспособным.
Это код услуги
app.service("IndexSummaryService", ['$http', function ($http) {
this.getIndexSummaryQueues = function (api) {
return $http.get(api, { cache: false });
};
}]);
Это контроллер
$scope.loadRecords = function (api) {
$scope.loading = true;
var GetIndexSummaryQueue = IndexSummaryService.getIndexSummaryQueues(api);
GetIndexSummaryQueue.then(function (response) {
$scope.Queues = response.data;
}, function (error) {
if (error.status == 500) {
$scope.errorStatus = "Error " + error.status;
$scope.errorMsg = error.data.message;
}
else {
$scope.errorStatus = "Error " + error.status;
$scope.errorMsg = GlobalConstants.errormessage;
}
$scope.errorpage = true;
$scope.success = false;
console.log("Status Data : " + error.data.message);
console.log("Status Error : " + error.status);
}).then(function() {
$scope.loading = false;
});
}
Я написал модульное тестирование в жасмине ниже код жасмина.
describe("ISummary ->", function() {
beforeEach(function() {
module("ApplicationModule");
});
var $httpBackend;
var scope, createController;
beforeEach(inject(function ($rootScope, _$httpBackend_, $controller) {
$httpBackend = _$httpBackend_;
scope = $rootScope.$new();
createController = function() {
return $controller('IndexingSummaryController', {
$scope: scope
});
};
$httpBackend.when("GET", "https://domain.com/captivaapi/api/capturestats/pldindexingSummary")
.respond([{ name: 'John', id: 1 }, { name: 'Josh', id: 2 }]);
}));
afterEach(function() {
$httpBackend.verifyNoOutstandingExpectation();
$httpBackend.verifyNoOutstandingRequest();
});
describe("Service->", function() {
it("can load topics", inject(function (IndexSummaryService) {
$httpBackend.expectGET("https://domain.com/captivaapi/api/capturestats/pldindexingSummary");
IndexSummaryService.getIndexSummaryQueues('https://domain/captivaapi/api/capturestats/pldindexingSummary');
$httpBackend.flush();
expect(IndexSummaryService.getIndexSummaryQueues.length).toBeGreaterThan(0);
expect(IndexSummaryService.getIndexSummaryQueues.length).toEqual(2);
expect(IndexSummaryService.getIndexSummaryQueues).toEqual([{ name: 'John', id: 1 }, { name: 'Josh', id: 2 }]);
}));
});
Thank you Lee..it работал..а незначительные изменения, которые были добавлены..res.data.length и res.data .. –