我正在尝试为我的应用程序编写单元测试。它包含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请求返回一个非常复杂的对象,我不知道怎么写我的测试。任何人都可以帮助我吗?非常感谢!