[英]Jasmine unit test controller
我需要在這里檢查兩件事:
測試失敗,消息:期望函數等於[object({name:'john',id:1}),object({name:'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 }]);
}));
});
您沒有測試諾言的響應,請嘗試以下操作(自從我使用Angular以來已經有一段時間了,可能不太准確,但是一旦諾言得到解決,基本上就在then塊中聲明)。
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').then(function(res) {
expect(res.length).toBeGreaterThan(0);
expect(res.length).toEqual(2);
expect(res).toEqual([{ name: 'John', id: 1 }, { name: 'Josh', id: 2 }]);
});
$httpBackend.flush();
}));
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.