意外请求:接下来是[$ Rootscope:Inprog] $ impect的$摘要.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时,出乎意料的GET和$digest已经在进行错误。我该如何解决此问题,请告诉我。

在任何时间点都可以有一个 $digest或$ apply操作正在进行中。这是为了防止很难检测到错误输入您的应用程序。此错误的堆栈跟踪使您可以追踪当前执行的$apply$digest调用的原点,这会导致错误。

请参阅此链接

最新更新