1

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");

    });

});
Delian Mitankin
  • 3,691
  • 26
  • 24
  • Usually mocking is being done through the `browser.addMockModule` method, so that each time you perform `browser.get` your module is loaded along with the page. Please provide the code that gives you trouble. – Delian Mitankin Jun 26 '15 at 06:13
  • Please see edited notes above. Thanks for your help. – user3715752 Jun 29 '15 at 05:04
  • As you can see, each test has exactly the same setup. I just separated it this way because my actual tests have different expectations and response data. But regardless, the exact code above will pass on the first test but the second will not let me login. And looking at fiddler, the second test is calling the real backend. – user3715752 Jun 29 '15 at 05:57
  • Does your login-procedure sets a cookie? If so, the conditions on the second test would be different than the first one (maybe you are already logged in). What's the url of the call in the second test that you are intercepting? Having that second `beforeEach` registers a module that is already registered. Possibly there is an angular error about that. Try removing the `beforeEach` in the second test. – Delian Mitankin Jun 29 '15 at 06:06
  • possible duplicate of [How to switch between httpBackendMocks in protractor test](http://stackoverflow.com/questions/29377619/how-to-switch-between-httpbackendmocks-in-protractor-test) – Paul Sweatte Jul 24 '15 at 13:42

1 Answers1

0

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.

ngstschr
  • 2,119
  • 2
  • 22
  • 38