Winkelmesser addMockModule und $ httpProvider interceptor

Diese Frage ist eine mögliche Lösung für meine andere Frage (wo sie Ratschläge zur Verwendung gibt)addMockModule vom Winkelmesser):Rufen Sie andere API auf, wenn Sie Tests mit Protractor ausführen.

Ich habe folgende Datei:mockedRest.js Dies ist das Modul, das ich zum Winkelmesser hinzufügen möchte. Es sollte alle REST-Aufrufe abfangen und die Adresse ersetzen (api / to apiMock /).

exports.apiMockModule = function () {

    console.log('apiMockModule executing');

    var serviceId = 'mockedApiInterceptor';
    angular.module('apiMockModule', ['myApp'])
        .config(['$httpProvider', configApiMock])
        .factory(serviceId,
        [mockedApiInterceptor]);

    function mockedApiInterceptor() {
        return {
            request: function (config) {
                console.log('apiMockModule intercepted');
                if ((config.url.indexOf('api')) > -1) {
                    config.url.replace('api/', 'apiMock/');
                }

                return config;
            },
            response: function (response) {
                return response
            }
        };
    }

    function configApiMock($httpProvider) {
        $httpProvider.interceptors.push('mockedApiInterceptor');
    }
};

Dann habe ich meinen eigentlichen Test wo ich das Modul lade.

describe('E2E addMockModule', function() {
    beforeEach(function() {
        var mockModule = require('./mockedRest');
        browser.addMockModule('apiMockModule', mockModule.apiMockModule);
        console.log('apiMockModule loaded');
        browser.get('#page');
    });
    it('tests the new apiMock', function() { 
        // test that clicks a button that performs a rest api call. left out as I can see the call in fiddler.
    });
});

Der REST-Aufruf zeigt jedoch weiterhin auf 'api /' anstelle von 'apiMock /'. Ich weiß nicht, ob ich mehr tun muss, damit der Interceptor seine Arbeit erledigt. Beachten Sie auch, dass in apiMockModule nichts an der Konsole protokolliert ist, so als würde das Modul nicht geladen.

Jeder Rat wird geschätzt.

Antworten auf die Frage(1)

Ihre Antwort auf die Frage