0
我想在我的情況,以測試rootscope http請求如何在我的情況下測試一個http請求
我有類似
mainCont文件
$rootScope.testLoad = $http.get('/api/testapi/product');
TestCont文件
$rootScope.testLoad.success(function(product){
console.log(product)
})
測試文件
describe('test', function() {
var $httpBackend, $rootScope, scope, mainCont, testCont;
beforeEach(module('myApp'));
beforeEach(inject(function (_$controller_, _$httpBackend_, _$rootScope_) {
scope = _$rootScope_.$new();
$rootScope = _$rootScope_;
$httpBackend = _$httpBackend_;
testCont = _$controller_('testCont', {
$scope: scope
});
mainContr = _$controller_('mainCont', {
$scope: scope
});
}));
describe('test http request', function() {
it('should check if the api is called', function() {
$httpBackend.expectGET('/api/testapi/product').respond(200);
//I am not sure how to test the call in testCont file.
})
})
});
我不知道如何測試在testCont來電,因爲當我運行測試, 我得到了一個錯誤說
TypeError: 'undefined' is not an object (evaluating '$rootScope.testLoad.success')
誰能幫我解決這個問題?非常感謝!
感謝您的幫助。我仍然得到相同的錯誤+1 – BonJon
我不熟悉你的測試設置。你確定你正在''rootScope'注入mainCont和testCont文件嗎? – tyler
我會這麼認爲,我有另一個測試文件,可以用這樣的設置工作 – BonJon