首页 > 解决方案 > 意外请求:GET 后跟 [$rootScope:inprog] $digest 已经在进行中。Angularjs 和 Karma

问题描述

规格文件

'use strict';

describe('component test : home', function () {

    // add module reference you want to test
    beforeEach(module('homeView'));

    // add templates [from karma]
    beforeEach(module('templates'));

    var element;
    var scope;
    var $httpBackend;

    beforeEach(inject(function ($rootScope, $compile, _$httpBackend_) {
        $httpBackend = _$httpBackend_;
        scope = $rootScope.$new();
        element = angular.element('<home-view></home-view>');
        scope.$apply(function () {
            $compile(element)(scope);
        });
    }));


    it('extra column http get', function () {
        var mockResponse = {
            data: {
                success: true,
                message: ':D :D'
            }
        };

        $httpBackend.expectGET('http://localhost:51275/api/ExtraColumn')
            .respond(mockResponse);

        expect(data).toEqual(mockResponse);

        $httpBackend.flush();
    });


    //it('header text', function () {
    //    var title = element.find('h1');
    //    console.log(title.text());
    //    expect(title.text()).toContain('Interviewees');
    //});



});

错误

 Error: Unexpected request: GET http://localhost:51275/api/ExtraColumn
        No more request expected
            at createFatalError (Scripts/angular-mocks.js:1569:19)
            at $httpBackend (Scripts/angular-mocks.js:1616:11)
            at sendReq (Scripts/angular.js:13257:9)
            at serverRequest (Scripts/angular.js:12998:16)
            at processQueue (Scripts/angular.js:17914:37)
            at Scripts/angular.js:17962:27
            at Scope.$digest (Scripts/angular.js:19075:15)
            at ChildScope.$apply (Scripts/angular.js:19463:24)
            at UserContext.<anonymous> (home-view/home-view.component.spec.js:19:15)
            at Object.invoke (Scripts/angular.js:5122:19)
            at <Jasmine>
            at window.inject.angular.mock.inject (Scripts/angular-mocks.js:3422:25)
            at Suite.<anonymous> (home-view/home-view.component.spec.js:15:16)
            at <Jasmine>
            at home-view/home-view.component.spec.js:3:1
        ReferenceError: data is not defined
            at <Jasmine>
            at UserContext.<anonymous> (home-view/home-view.component.spec.js:36:16)
            at <Jasmine>

我正在尝试为我的 angularjs 应用程序编写测试。测试需要模拟一个 http 请求,我在上面写了这个并得到了这些错误。$digest当我的代码中什至没有 a 时,出现意外的 get 和已经在进行的错误$digest。我该如何解决这个问题,请告诉我。

标签: angularjs

解决方案


在任何时间点,都只能有一个$digest或 $apply 操作在进行中。这是为了防止很难检测到的错误进入您的应用程序。此错误的堆栈跟踪允许您跟踪导致错误的当前执行$apply$digest调用的来源。

请参考此链接


推荐阅读