Jasmine Controller test: $timeout.flush() causes an 'Unexpected GET request' error

时光总嘲笑我的痴心妄想 提交于 2019-12-07 08:52:25

问题


I have the following test:

it('should load productGroups into the scope', function(){
  scope._section = 'modifiers';
  scope.userData = {method: 'manual'};
  scope.$digest();
  timeout.flush();//causes the error

  expect(scope.productGroups).toEqual(productGroupService.getProductGroups());
});

Now, the action that I am trying to test occurs within a timeout of 0, because of some issues I had with syncing the data stored in the cookies.

Now, without the marked line, the test runs find, except the expected result is not obtained. With the marked line, I get the following error:

    Error: Unexpected request: GET views/main.html
    No more request expected
        at $httpBackend (/home/oleg/dev/vita-webapp-new/bower_components/angular-mocks/angular-mocks.js:1178:9)
        at sendReq (/home/oleg/dev/vita-webapp-new/bower_components/angular/angular.js:8180:9)
        at $http.serverRequest (/home/oleg/dev/vita-webapp-new/bower_components/angular/angular.js:7921:16)
        at wrappedCallback (/home/oleg/dev/vita-webapp-new/bower_components/angular/angular.js:11319:81)
        at wrappedCallback (/home/oleg/dev/vita-webapp-new/bower_components/angular/angular.js:11319:81)
        at /home/oleg/dev/vita-webapp-new/bower_components/angular/angular.js:11405:26
        at Scope.$eval (/home/oleg/dev/vita-webapp-new/bower_components/angular/angular.js:12412:28)
        at Scope.$digest (/home/oleg/dev/vita-webapp-new/bower_components/angular/angular.js:12224:31)
        at Scope.$apply (/home/oleg/dev/vita-webapp-new/bower_components/angular/angular.js:12516:24)
        at Object.fn (/home/oleg/dev/vita-webapp-new/bower_components/angular/angular.js:14023:36)


Process finished with exit code 0

main.html, is obviously the view of this controller, tried placing it in templateCache with the following code, but it did not help:

$templateCache.put('views/main.html', $templateCache.get('app/views/views/main.html'));

Any assistance would be much appreciated.


回答1:


Solution I've been able to come up with is:

$httpBackend.when('GET', 'views/main.html').respond('');

in the beforeEach function

Nothing else doesn't seem to work.

While it's a working solution for unit-testing purposes, it would never work for an E2E test.



来源:https://stackoverflow.com/questions/25711871/jasmine-controller-test-timeout-flush-causes-an-unexpected-get-request-err

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!