2

我有一个 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?');
            })
        });
    });
4

0 回答 0