willedanielsson
willedanielsson

Reputation: 1333

Protractor override "beforeAll" http mock

I'm using Protractor in order to get some End-to-End testing in my application. In a test, I mock the backend-calls with a MockModule as following:

describe('Lets test a feature' , function(){
    beforeAll(function () {
        var customerE2E = function () {
            angular.module('customerE2E', ['customer', 'ngMockE2E'])
                .run(function ($httpBackend) {

                    $httpBackend.whenPOST('/api/data').respond(200);
                    $httpBackend.whenGET(/^.*/).passThrough();
            });
        };
        browser.addMockModule('customerE2E', customerE2E);
    });

    it('correctly demonstrates my problem', function () {
        expect(element(by.css('h4')).getText()).toMatch('Hello world');
    }    
})

This works really good but my problem is that I also want to test my application when the post responds with a 404, 500 etc. I have solved this by having a new "describe"-function for each case but it would be nice to just be able to override this call from inside the "it"-functions.

it('correctly demonstrates my problem', function () {
    expect(element(by.css('h4')).getText()).toMatch('Hello world');
}

it('show error due to bad request', function () {
    /* 
     Replace $httpBackend.whenPOST('/api/data').respond(200);
     with $httpBackend.whenPOST('/api/data').respond(404);
    */
    expect(element(by.css('h4')).getText()).toMatch('Failed api call');
} 

I'm really new to this so I am wondering if there is a nice way to achieve an easy way to override the earlier MockModuled that was set in the beforeAll-function.

Upvotes: 0

Views: 346

Answers (1)

Sudharsan Selvaraj
Sudharsan Selvaraj

Reputation: 4832

You can use beforeEach() to achieve it.

describe('Test Mock Module',function () {

    var statusCodes = [200,404,500];
    var currentTestNumber = 0;
    beforeAll(function () {
        var customerE2E = function () {
            angular.module('customerE2E', ['customer', 'ngMockE2E'])
                .run(function ($httpBackend) {

                    $httpBackend.whenPOST('/api/data').respond(200);
                    $httpBackend.whenGET(/^.*/).passThrough();
                });
        };
        browser.addMockModule('customerE2E', customerE2E);
    });

    /*Below method will be executed before each test and set the required response respectively*/
    beforeEach(function () {
        $httpBackend.whenPOST('/api/data').respond(statusCodes[currentTestNumber++]);
    });

    it('test 200 status code',function () {
        expect(element(by.css('h4')).getText()).toMatch('Message for 200 status code');
    });

    it('test 404 status code',function () {
        expect(element(by.css('h4')).getText()).toMatch('Message for 404 status code');
    });

    it('test 500 status code',function () {
        expect(element(by.css('h4')).getText()).toMatch('Message for 500 status code');
    });
});

Upvotes: 2

Related Questions