vendredi 16 décembre 2016

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.

Aucun commentaire:

Enregistrer un commentaire