Ich muss hier zwei, was überprüfen:Jasmine Unit-Test-Controller
- Die Antwortlänge. spezifizierte die Scheindaten als [{Name: 'John', ID: 1}, {Name: 'Josh', ID: 2}], so dass die Antwortlänge 2 sein sollte. Bei diesem Test wird immer die Länge als 1 erhalten.
- Die Antwortdaten sollten gleich sein. dh. expect (IndexSummaryService.getIndexSummaryQueues) .toEqual ([{Name: 'John', id: 1}, {Name: 'Josh', id: 2}]);
Der Test schlägt fehl mit der Meldung: Erwartete Funktion gleich [Objekt ({name: 'John', id: 1}), Objekt ({name: 'josh', id: 2})]
Mein Service ist etwas anders, was api als Parameter annimmt, der die URL ist.
Bitte schlagen Sie vor, wie Sie diesen Komponententest durchführen können.
Dies ist der Service-Code
app.service("IndexSummaryService", ['$http', function ($http) {
this.getIndexSummaryQueues = function (api) {
return $http.get(api, { cache: false });
};
}]);
Dies ist der Controller haben
$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;
});
}
I Unit-Test unten in Jasmin geschrieben ist der Jasmin-Code ist.
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 }]);
}));
});
Danke Lee..es hat funktioniert..eine kleine Änderung, die hinzugefügt wurde..res.data.length und res.data .. –