Why subsequent tests for ngmocke2e failed to call mock backend? Only the first test would pass. Second test would call the real backend.

Here is my sample code: The first test would call the mock. The second will call the real backend.

var LoginPage = require( './login_page' );

describe('As a user, when using valid credentials', function() {

    var login = new LoginPage();

    beforeEach(
        function () {

            browser.addMockModule('myService', function() {
                angular
                    .module('myService', ['myApp', 'ngMockE2E'])
                    .run(function($httpBackend) {

                        var access={"access_token":"a", "token_type":"bearer", "expires_in":299, "refresh_token":"a", "userName":"any", "clientId":"blah", ".issued":"Mon, 08 Jun 2015 20:47:40 GMT", ".expires":"Mon, 08 Jun 2015 20:52:40 GMT"};

                        $httpBackend.whenPOST("https://blah.com/OAuth2Server/1.0/OAuth/Token").respond(200, access);

                        $httpBackend.whenGET(/\/*/).passThrough();
                        $httpBackend.whenPOST().passThrough();

                    });
            });
        });


    it('logins successfully', function() {
            login
            .navigate()
            .login("anything", "password");

        browser.sleep(5000);
        browser.ignoreSynchronization=true;

        var currentUrl=browser.getCurrentUrl();
        expect(currentUrl).toBe("http://localhost:55555/#/my-jobs");

    });

});

describe('As a user, when using valid credentials', function() {

    var login = new LoginPage();

    beforeEach(
        function () {

            browser.addMockModule('myService', function() {
                angular
                    .module('myService', ['myApp', 'ngMockE2E'])
                    .run(function($httpBackend) {

                        var access={"access_token":"a", "token_type":"bearer", "expires_in":299, "refresh_token":"a", "userName":"any", "clientId":"blah", ".issued":"Mon, 08 Jun 2015 20:47:40 GMT", ".expires":"Mon, 08 Jun 2015 20:52:40 GMT"};

                        $httpBackend.whenPOST("https://blah.com/OAuth2Server/1.0/OAuth/Token").respond(200, access);

                        $httpBackend.whenGET(/\/*/).passThrough();
                        $httpBackend.whenPOST().passThrough();

                    });
            });
        });


    it('logins successfully', function() {
            login
            .navigate()
            .login("anything2", "password2");

        browser.sleep(5000);
        browser.ignoreSynchronization=true;

        var currentUrl=browser.getCurrentUrl();
        expect(currentUrl).toBe("http://localhost:55555/#/my-jobs");

    });

});
1

There are 1 answers

0
ngstschr On

I don't exactly know why this issue occurs (happened to me as well), but I was able to 'fix' it by restarting the browser at the end each describe. Just reloading it didn't work for me.

Add the following inside each describe:

afterAll(function() {
    // need this to avoid problems with ngmocke2e
    browser.restart();
});

I know it's not an ideal solution, and it adds some extra time to the tests, but this does the trick for now.