量角器addMockModule和$ httpProvider拦截器

时间:2014-06-17 09:05:57

标签: angularjs protractor angularjs-e2e

这个问题是我的另一个问题的可能解决方案(他们建议使用量角器中的addMockModule):Call other api when running tests using Protractor

我有以下文件:mockedRest.js这是我要添加到量角器的模块。它应该拦截任何REST调用并替换地址(api /到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');
    }
};

然后我在实际测试中加载模块。

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.
    });
});

然而,REST调用仍然指向'api /'而不是'apiMock /' 我不知道是否必须做更多的事情才能让拦截器完成它的工作。 还值得注意的是,apiMockModule中没有任何记录到控制台,就像它没有加载模块一样。

感谢任何建议。

1 个答案:

答案 0 :(得分:11)

我在模拟模块中修复了两个小错误,以使其有效。

  • 模拟模块不需要依赖于您的应用程序,
  • config.url必须由转换后的那个设置。

更新后的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');
  }
};

我已在此环境中测试过此代码:

  • 量角器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

您写道:

  

apiMockModule

中没有任何记录到控制台的内容

这是正常的,模块代码不是由量角器执行,而是发送到浏览器(使用driver.executeScript)。所以代码由浏览器执行。

但可以get the logs from the browser进行调试:

...
it('tests the new apiMock', function() {
  browser.manage().logs().get('browser').then(function(browserLog) {
    console.log('log: ' + require('util').inspect(browserLog));
  });
...
相关问题