Protractor addMockModule and $httpProvider interceptor

不羁岁月 提交于 2019-11-30 19:13:29

I make two minor bug fixes in mock module to make it works.

  • the mock module don't need to depend on your application,
  • the config.url have to be set by the transformed one.

The updated mockedRest.js :

exports.apiMockModule = function () {

  console.log('apiMockModule executing');

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

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

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

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

I have tested this code in this environment:

  • protractor 0.24.1
  • angular 1.2.16
  • ChromeDriver 2.10.267517
  • Google chrome 35.0.1916.153
  • Mac OS X 10.9.3 x86_64

You wrote:

There isn't anything logged to console inside the apiMockModule

It is normal, the module code is not executed by protractor, but sent to the browser (using driver.executeScript). So the code is executed by the browser.

But it is possible to get the logs from the browser for debugging:

...
it('tests the new apiMock', function() {
  browser.manage().logs().get('browser').then(function(browserLog) {
    console.log('log: ' + require('util').inspect(browserLog));
  });
...
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!