使用 Protractor 运行测试时调用其他 api [英] Call other api when running tests using Protractor

查看:19
本文介绍了使用 Protractor 运行测试时调用其他 api的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前使用量角器设置了一些测试.这些测试从 WebApi 检索数据.但是,我想将 WebApi 调用指向一个真实但模拟的 WebApi,它只返回我想用作测试数据的对象.

I currently have setup some tests using protractor. These tests are retrieving data from a WebApi. However, I want to point the WebApi calls to a real but mocked WebApi that just returns the objects I want to use as test data.

例如当前请求为:

http://localhost/myApp/api/profile/getUser/1

我想操纵这个请求,以便测试将使用:

I want to manipulate this request so the test will use:

http://localhost/myApp/apiMock/profile/getUser/1

首先,我想编写一个拦截器来更改请求标头,但我不知道如何使用 Protractor 使其工作.我不能在量角器测试中直接使用 angular,可以吗?如果可以,编写拦截器不是问题,我只是不知道如何将其插入量角器中进行测试.

First I thought to write an interceptor that changes the request header but I don't have a clue how to make this work with Protractor. I can't use angular directly within my protractor tests, can I? If I can, writing the interceptor isn't a problem, I just don't know how to plug it in for my tests in protractor.

我已阅读以下帖子:E2E 和 Mocking 但它不能满足我的需求,因为我不想一遍又一遍地调用相同的 url,我想替换(部分)动态 url 指向模拟的 api 服务.

I've read the following post: E2E and Mocking but it doesn't fulfill my needs as I don't want to call the same url over and over again, I want to replace (part of) the dynamic url to point to the mocked api service.

为了让自己更清楚,我不是在客户端寻找模拟数据.我想保留 API 调用(在我看来它更像是 E2E)但只是指向另一个返回模拟数据的 API url.

Just to make myself more clear, I'm not looking for mocked data on the client side. I want to keep the API calls (in my opinion it's more E2E) but just point to another API url that returns mocked data.

推荐答案

添加一个addMockModule

更多详情:量角器 addMockModule 和 $httpProvider 拦截器

解决方案:

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

然后我在加载模块的地方进行了实际测试.

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() { 
        // your test that clicks a button that performs a rest api call. 
    });
});

感谢@gontard

这篇关于使用 Protractor 运行测试时调用其他 api的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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