【问题标题】:Mocking the HTTP backend in JavaScript protractor end to end testing在 JavaScript 量角器端到端测试中模拟 HTTP 后端
【发布时间】:2015-12-04 11:18:32
【问题描述】:

我有一些端到端测试(JavaScript + Protractor),我需要模拟一个 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() 函数!)

【问题讨论】:

  • 您是否已将angular-mocks.js 添加到您的页面 HTML 中?
  • 您能否提供实际的堆栈跟踪(如果有)?

标签: javascript unit-testing mocking protractor e2e-testing


【解决方案1】:

我也在使用 http-backend-proxy。

我需要这个来传递(主要是 html 页面作为它的 Angular 应用程序):

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

还有onLoad:

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

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

【讨论】:

    猜你喜欢
    • 2023-04-08
    • 2016-01-24
    • 2018-05-28
    • 2014-08-04
    • 2017-11-30
    • 1970-01-01
    • 1970-01-01
    • 2022-07-13
    • 1970-01-01
    相关资源
    最近更新 更多