我正在嘗試爲我的應用程序編寫單元測試。它包含http請求呼叫在我的情況。如何在單元測試中檢查http請求?
測試文件
'use strict';
describe('Controller: testCtrl', function() {
// load the controller's module
beforeEach(module('myApp'));
var testCtrl, scope, $httpBackend;
beforeEach(inject(function (_$controller_, _$rootScope_, _$httpBackend_, $cookies) {
scope = _$rootScope_.$new();
$httpBackend = _$httpBackend_;
testCtrl = _$controller_('testCtrl', {
$scope: scope,
});
}));
it("should return product data", function() {
$httpBackend.whenGET('/api/store/' + productID + '/products').respond([{
//not sure what I should do next...
}])
})
控制器文件
$http.get(('/api/store/' + productID + '/products').success(
function(data) {
//the data could contain 10 objects with 10+ property within each object.
}
);
由於HTTP請求返回一個非常複雜的對象,我不知道怎麼寫我的測試。任何人都可以幫助我嗎?非常感謝!