我有一个angularjs指令,它使用ng-mouseenter来触发对后端数据的调用。如果对后端的调用超过一定时间,则会显示加载指示符。
我想测试当网络通话花费的时间超过指定的时间段时,会显示加载指示符。
有没有办法用$ httpBackend模拟这个延迟?
触发这些操作的代码(在指令中):
scope.triggerPopover = function () {
if (angular.element('.my-element-style').length === 0) {
scope.detailProm = $timeout(function () {
makeRESTCall(attrs.assetId || '');
}, 200); //waits this length before it makes any call to the backend
//we want the loading icon to come a little bit later, because it will seem to flicker
//when calls are fast
scope.loadingProm = $timeout(function () {
showLoadingPopover();
}, 500);
}
};
html /指令使用
<article my-directive ng-mouseenter="triggerPopover();"></article>
在我在beforeEach()中的规范......
//mock the backend call
$httpBackend.whenGET('/my/rest/endpoint')
.respond(mockData);
悬停测试:
describe('hover tests', function () {
it('hovering over the article should make a loading element appear while waiting for a restangular call', function () {
runs(function() {
//it takes a few milliseconds for the mouseover to react
//due to a timeout in the directive
jQuery(jQuery('the-class-im-looking-for')[0]).mouseover();
});
waits(1000);
runs(function() {
console.log('I want to know if the loading popover was launched, but I need to set a delay in the $httpBackend?');
})
});
});