lundi 26 octobre 2015

Unit tests failing when AngularJs upgraded from 1.2.29 to 1.4.7

Upgrading AngularJS from 1.2.29 to 1.4.7 for our app. The functional code works just fine, and is noticeably snappier than before. When I run the unit tests I get the following errors. In both cases, its pointing to the $httpBackend.flush() as the location of the error:

1) expect subsManager to get subscriptions
     MyModule subsManager
     Error: [$rootScope:inprog] $digest already in progress
http://ift.tt/1WetWrH
...
2) expect subsManager get customer Id to fail
     MyModule subsManager
     Error: [$rootScope:inprog] $digest already in progress
http://ift.tt/1WetWrH
...

The test cases are invoking a factory, which is basically making $http requests, not dealing with $scope.$digest or $apply. These tests worked properly before upgrading to AngularJS 1.4.7. Here's the subs-mgr.spec.js (modified to slightly obscure the real variable names...):

/*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();
        }));
    });
});

I upgraded all the supporting packages as well, so angular and angular-mocks are both at 1.4.7. From "bower list":

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

Any pointers as to what I'm doing wrong?

Aucun commentaire:

Enregistrer un commentaire