量角器addMockModule和$ httpProvider拦截 [英] Protractor addMockModule and $httpProvider interceptor

查看:151
本文介绍了量角器addMockModule和$ httpProvider拦截的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题是我的其他问题可能的解决方案(在那里他们建议使用 addMockModule 从量角器):<一href=\"http://stackoverflow.com/questions/24244803/call-other-api-when-running-tests-using-protractor\">Call运行使用量角器测试时,其他的API。

This question is a possible solution for my other question (where they advice to use addMockModule from protractor): Call other api when running tests using Protractor.

我有以下文件: mockedRest.js 这就是我想要添加到量角器模块。它应该截获任何REST调用并更换地址(API /到apiMock /).

I have the following file: mockedRest.js this is the module I want to add to protractor. It should intercept any REST calls and replace the address (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');
    }
};

然后,我有我的实际测试中,我加载模块。

Then I have my actual test where I load the module.

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控制台里面一样,它不是加载模块。

However, the REST call is still pointing to 'api/' instead of 'apiMock/' I don't know if I have to do anything more in order to get the interceptor to do its work. It's also worth to note that there isn't anything logged to console inside the apiMockModule, like it isn't loading the module.

任何意见是AP preciated。

Any advice is appreciated.

推荐答案

我做模拟模块中的两个小错误修正,使之工作。

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


  • 模拟模块并不需要依赖于你的应用程序,

  • 的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');
  }
};

我在这样的环境中测试了这个code:

I have tested this code in this environment:


  • 量角器0.24.1

  • 角1.2.16

  • ChromeDriver 2.10.267517

  • 谷歌chrome 35.0.1916.153

  • 的Mac OS X 10.9.3的x86_64

您写道:

有没有什么记录到apiMockModule内部控制台

There isn't anything logged to console inside the apiMockModule

这是正常的,该模块code不被量角器执行,而是发送到浏览器(使用<一个href=\"https://github.com/angular/protractor/blob/master/lib/protractor.js#L932-L941\"><$c$c>driver.executeScript).因此,code通过浏览器执行。

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.

但它有可能对<一href=\"https://github.com/angular/protractor/blob/master/docs/faq.md#how-can-i-get-hold-of-the-browsers-console\">get从浏览器的日志中进行调试:

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

这篇关于量角器addMockModule和$ httpProvider拦截的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆