我们正在使用
ng-apimock
测试我们的角度应用。为了在某些测试中返回特定的数据集,我们使用
ngApiMock.selectScenario
当我查看selectScenario(在tcp监视器中)的http请求时,我看到一个头部添加了key
ngapimockid
(和生成的值)。
然而,在执行测试时,我注意到
ngapimockid
通常未在标头中指定(或作为cookie),导致在
ngApiMock。选择方案
.
例如,测试执行期间的http请求(使用ngapimockid):
GET /somerandomuri HTTP/1.1
cookie: ngapimockid=46206072-c0e3-44f3-b93c-4e1658db9d46
accept-language: en-US,en;q=0.9
accept-encoding: gzip, deflate, br
referer: http://localhost:49152/
user-agent: Moz.......
以及在同一测试执行期间http请求的示例
ngapimockid
(到相同端点):
GET /somerandomuri HTTP/1.1
accept-language: en-US,en;q=0.9
accept-encoding: gzip, deflate, br
referer: http://localhost:49152/
user-agent: Moz.......
我想
ngapimockid
是为了支持不同场景的并行测试,但我不明白为什么在测试执行期间经常不添加它。
我认为以下部分
protractor.mock.js
负责添加用于添加
ngapimockid
测试http请求:
/** Make sure that angular uses the ngapimock identifier for the requests. */
browser.getProcessedConfig().then((config) => {
// As of protractor 5.0.0 the flag config.useAllAngular2AppRoots has been deprecated, to let protractor tell
// ngApimock that Angular 2 is used a custom object needs to be provided with the angular version in it
// See: https://github.com/angular/protractor/blob/master/CHANGELOG.md#features-2
if (config.useAllAngular2AppRoots || ('ngApimockOpts' in config && config.ngApimockOpts.angularVersion > 1)) {
// angular 2 does not have addMockModule support @see https://github.com/angular/protractor/issues/3092
// fallback to cookie
require('hooker').hook(browser, 'get', {
post: function (result) {
return result.then(function () {
// Since protractor 5.0.0 the addCookie is an object, see
// https://github.com/angular/protractor/blob/master/CHANGELOG.md#500
try {
return browser.manage().addCookie({name: "ngapimockid", value: ngapimockid});
} catch (error) {
// Fallback protractor < 5.0.0
return browser.manage().addCookie('ngapimockid', ngapimockid);
}
});
}
});
// Angular 2+ lacks addMockModule, but hybrid apps still need this
if(!!config.ngApimockOpts.hybrid) {
browser.addMockModule('ngApimock', ProtractorMock, {ngapimockid: ngapimockid});
}
} else {
browser.addMockModule('ngApimock', ProtractorMock, {'ngapimockid': ngapimockid})
}
同样,我不明白为什么它不能适用于所有http请求。我是否缺少一些解释这种行为的关键配置/设置?