$ httpBackend Angular导致测试失败并显示“预期没有更多请求”

时间:2018-08-07 16:03:31

标签: angularjs unit-testing jasmine karma-jasmine

在重构单元测试依赖项期间,我遇到了这个错误,它发生在大约200多个测试上,这些测试依赖于带有$ httpBackend的角度模拟,导致它们失败。我查看了调用堆栈,似乎$ httpBackend.flush()导致它们在尝试trySync时失败。

这是失败的测试的样子,这就是错误的样子:

失败测试:

fdescribe('UserPanelSettingsCtrl', function() {
   var $q, userPanelSettingsCtrl, scope, $httpBackend, WEBAPI_URL, MDAPI_URL, jstz;

beforeEach(function() {
    module('config');
    module('Core');
    module('utils');
});

beforeEach(function() {
    inject(function(_$q_, _$controller_, _$httpBackend_, _WEBAPI_URL_, _MDAPI_URL_, _$rootScope_, _jstz_) {
        $q = _$q_;
        scope = _$rootScope_.$new();
        $httpBackend = _$httpBackend_;
        WEBAPI_URL = _WEBAPI_URL_;
        MDAPI_URL = _MDAPI_URL_;
        jstz = _jstz_;

        userPanelSettingsCtrl = _$controller_('UserPanelSettingsCtrl', {
            $scope: scope,
            user: {'profile':{'workgroups':{}}},
            jstz: jstz
        });
    });
});

beforeEach(function(){
    $httpBackend.expectGET(WEBAPI_URL + 'v1/users/me/settings/webui').respond({});
    $httpBackend.expectGET(MDAPI_URL + "v1/list/Mapped%20Megacomponent").respond([{id:1,name:'3D'}]);
    $httpBackend.expectGET(MDAPI_URL + "v1/list/Mapped%20Component").respond([{id:1,name:'D3D'}]);
});

afterEach(function(){
    angular.element('.toast').remove();
});

it('should load user settings and profile data from API ', function() {
    $httpBackend.flush();

    expect(scope.loading).toBe(false);
    expect(scope.verticals[0].id).toEqual(1);
    expect(scope.components[0].id).toEqual(1);
});
it('should save profile data to the API ', function() {

    var deferred = $q.defer();
    deferred.resolve();
    scope.user.updateWorkgroups = function(){ return deferred.promise; };
    scope.saveUserProfile({});
    $httpBackend.expectGET(WEBAPI_URL + 'v2/users/me').respond({});
    $httpBackend.expectPUT(WEBAPI_URL + 'v1/users/me/settings/webui').respond({});
    $httpBackend.flush();
    scope.$digest();

    expect(angular.element('.toast')).toBeDefined();
    expect(angular.element('.toast-message').text()).toEqual('User profile updated');

});`

这就是错误的样子:

Chrome 67.0.3396 (Windows) UserPanelSettingsCtrl should load user settings and profile data from API  FAILED
    Error: Unexpected request: GET /api/web/v2/users/me
    No more request expected
        at $httpBackend (C:/Users/usr/Desktop/Karma Work - Copy/src/webui/public/bower_components/angular-mocks/angular-mocks.js:1419:9)
        at sendReq (C:/Users/usr/Desktop/Karma Work - Copy/src/webui/public/bower_components/angular/angular.js:12027:9)
        at serverRequest (C:/Users/usr/Desktop/Karma Work - Copy/src/webui/public/bower_components/angular/angular.js:11822:16)
        at processQueue (C:/Users/usr/Desktop/Karma Work - Copy/src/webui/public/bower_components/angular/angular.js:16696:28)
        at C:/Users/usr/Desktop/Karma Work - Copy/src/webui/public/bower_components/angular/angular.js:16712:27
        at Scope.$eval (C:/Users/usr/Desktop/Karma Work - Copy/src/webui/public/bower_components/angular/angular.js:17994:28)
        at Scope.$digest (C:/Users/usr/Desktop/Karma Work - Copy/src/webui/public/bower_components/angular/angular.js:17808:31)
        at Scope.$apply (C:/Users/usr/Desktop/Karma Work - Copy/src/webui/public/bower_components/angular/angular.js:18102:24)
        at done (C:/Users/usr/Desktop/Karma Work - Copy/src/webui/public/bower_components/angular/angular.js:12082:47)
        at Array.handleResponse (C:/Users/usr/Desktop/Karma Work - Copy/src/webui/public/bower_components/angular-mocks/angular-mocks.js:1369:9)

不确定如何解决此问题,我尝试逐步解决此问题,但无法缩小问题范围。可能是一个依赖问题,因为我一直在更改依赖关系所在的文件结构,并且可能错过了构建应用程序的grunt文件中的某些内容。

1 个答案:

答案 0 :(得分:0)

您期望在调用GET后使用saveUserProfile网址调用API的/api/web/v2/user/me。因此,如下所示向上移动$httpBackend.expectGET

it('should save profile data to the API ', function() {
    $httpBackend.expectGET(WEBAPI_URL + 'v2/users/me').respond({});
    $httpBackend.expectPUT(WEBAPI_URL + 'v1/users/me/settings/webui').respond({});

    var deferred = $q.defer();
    deferred.resolve();
    scope.user.updateWorkgroups = function(){ return deferred.promise; };
    scope.saveUserProfile({});

    $httpBackend.flush();
    scope.$digest();

    expect(angular.element('.toast')).toBeDefined();
    expect(angular.element('.toast-message').text()).toEqual('User profile updated');
});