在 JavaScript 量角器端到端测试中模拟 HTTP 后端

Mocking the HTTP backend in JavaScript protractor end to end testing

我有一些端到端测试(JavaScript + 量角器),我需要模拟一个 API 后端。

我正在使用 http-backend-proxy:

我就是这么做的:

var HttpBackend = require('http-backend-proxy');
var myData = require('myFakeApiResponse.json');

this.proxy = new HttpBackend(browser);  
this.proxy.whenGET(/.+\/api\/groups\/.+/).respond(200, myData);

JavaScript 抱怨 whenGET 不是函数。

如果我这样做:

this.proxy.whenGET(/.+\/api\/groups\/.+/);

它不再抱怨了(但显然我需要设置响应,所以我需要 .respond() 部分)

我不明白为什么它不起作用。代理对象似乎已设置,当我 console.log 我得到:

{ when: [Function],
  whenGET: [Function],
  whenPUT: [Function],
  whenHEAD: [Function],
  whenPOST: [Function],
  whenDELETE: [Function],
  whenPATCH: [Function],
  whenJSONP: [Function],
  context: {},
  flush: [Function],
  syncContext: [Function],
  onLoad: [Getter] }

它似乎是一个合适的 JavaScript 对象(甚至列出了 whenGET() 函数!)

我也在使用 http-backend-proxy。

我需要这个来传递(主要是 html 页,因为它是一个 Angular 应用程序):

proxy.onLoad.whenGET(/.*/).passThrough();

还有 onLoad:

proxy.onLoad.whenGET('the url').respond(...);

当我使用 browser.get(...) 导航时,onLoad.whenGET 被调用。