当AngularJs从1.2.29升级到1.4.7时,单元测试失败

时间:2015-10-26 22:47:56

标签: javascript angularjs node.js unit-testing

为我们的应用程序将AngularJS从1.2.29升级到1.4.7。功能代码工作正常,并且明显比以前更快捷。当我运行单元测试时,我得到以下错误。在这两种情况下,它指向$ httpBackend.flush()作为错误的位置:

1) expect subsManager to get subscriptions
     MyModule subsManager
     Error: [$rootScope:inprog] $digest already in progress
http://errors.angularjs.org/1.4.7/$rootScope/inprog?p0=%24digest
...
2) expect subsManager get customer Id to fail
     MyModule subsManager
     Error: [$rootScope:inprog] $digest already in progress
http://errors.angularjs.org/1.4.7/$rootScope/inprog?p0=%24digest
...

测试用例正在调用一个工厂,它基本上是$ http请求,不处理$ scope。$ digest或$ apply。在升级到AngularJS 1.4.7之前,这些测试工作正常。 这是subs-mgr.spec.js(修改后略微模糊了真正的变量名......):

/*global describe, it, before, beforeEach, fixtures, module, angular, assert, inject, expect*/
"use strict";

describe("MyModule", function () {
    var LoggerMock = {
        getInstance: function () {
            return {
                error: function () {
                },
                log: function () {
                }
            };
        }
    };

    before(function () {
        fixtures.clearCache();
        fixtures.path = "base/tests/fixtures/";
    });

    beforeEach(module("MyModule"));

    beforeEach(module(function ($provide) {
        $provide.value("Logger", LoggerMock);
    }));

    beforeEach(angular.mock.inject(function ($injector, $httpBackend) {
        $httpBackend = $injector.get("$httpBackend");    
        try {
            $httpBackend.resetExpectations();
            $httpBackend.verifyNoOutstandingExpectation();
            $httpBackend.verifyNoOutstandingRequest();
        } catch (err) {/* do nothing */
        }
    }));

    describe("subsManager", function () {
        it("expect subsManager to get subscriptions", inject(function (subsManager, $httpBackend) {
            var data = {
                subs: JSON.parse(fixtures.read("subs.json"))
            };

            $httpBackend.expectGET( "currentCustomer").respond(200, "12345678");
            $httpBackend.expectGET( "customer/subs").respond(200, data);

            subsManager.getCustomerSubs().then(
                function (result) {
                    assert((result.length === 6), "Failed to receive array of 6 subscriptions.");
                },
                function (failureResponse) {
                    assert(false, "Should not receive FailureResponse");
                }
            );

            $httpBackend.flush();
        }));

        it("expect subsManager get customer Id to fail", inject(function (subsManager, $httpBackend) {
            $httpBackend.expectGET( "currentCustomer").respond(403, null);

            subsManager.getCustomerSubs().then(
                function (result) {
                    assert((result.length === 0), "Failed, array should have no data.");
                },
                function (failureResponse) {
                    assert(false, "Should not receive FailureResponse");
                }
            );
            $httpBackend.flush();
        }));
    });
});

我升级了所有支撑包,因此角度和角度模拟都是1.4.7。从“凉亭列表”:

bower check-new     Checking for new versions of the project dependencies..
├── angular#1.4.7 (latest is 1.5.0-build.4329+sha.773efd0)
...
├─┬ angular-mocks#1.4.7 (1.5.0-build.4329+sha.773efd0 available)
│ └── angular#1.4.7

关于我做错了什么的指示?

0 个答案:

没有答案