我知道两个这样的模拟框架可以帮助你。一个是ng-apimock,另一个是json-server。
我最近开始使用 ng-apimock API 来模拟一些后端 REST 调用。就我能够在这个 npm 库中看到一些有趣的功能而言,这似乎很好。在这里,您可以定义和选择场景和预设(多个模拟)并基本上配置哪个模拟用于哪个 e2e 测试用例。这基本上意味着通过根据需要提供基本响应数据来对 e2e 测试进行细粒度控制。互联网上很多博客都说,设置起来并不容易,这一点我可以肯定地证实。但它看起来像是我的用例的解决方案。
除了定义模拟和预设(可选)之外,我基本上必须设置一个 proxy.conf.json,并且还必须维护一些量角器配置以使用此 API。
基本上,您可以准确配置在 e2e 测试运行时应该从哪个 API 端点返回哪些值,并且还可以禁用更改应该为每个 e2e 测试用例返回的值。在此 API 中还有一个称为 passThrough 的选项,这意味着您甚至可以选择该方案以确保禁用模拟并将调用转到您的真实 HTTP 后端。
如果您想了解更多详细信息,请告诉我,我可能会为您提供有关如何配置它的详细信息。
更新(长时间发布警报!!):
模拟服务器设置(ng-apimock & protractor with express,同时)
模拟服务器.js
const express = require('express');
const apimock = require('@ng-apimock/core');
const devInterface = require('@ng-apimock/dev-interface');
const app = express();
app.set('port', (process.env.PORT || 30020));
apimock.processor.process({
src: 'mockapi', //name of the folder containing mocks and presets
watch: true
});
app.use(apimock.middleware);
app.use('/mocking', express.static(devInterface)); //endpoint for UI Dev Interface
app.listen(app.get('port'), function() {
console.log('mock app-server running on port', app.get('port'));
});
package.json(脚本部分)
"test:e2e": "ng e2e",
"start:mockapi": "node mockapi/mock-server.js",
"e2e": "concurrently -k -s first \"npm run start:mockapi\" \"npm run test:e2e\""
package.json(devDependencies 部分)
"@ng-apimock/core": "^2.6.0",
"@ng-apimock/dev-interface": "^1.1.0",
"@ng-apimock/protractor-plugin": "^1.1.0",
"ng-apimock": "^1.4.9",
"concurrently": "^6.0.1",
"express": "^4.17.1",
mock-proxy.conf.json
在 mocks 文件夹中添加了所需的代理配置文件。这是为了确保代理 http 后端调用转到正确的模拟服务器 Url。
为 UI 开发接口添加了额外的端点,可在开发期间用于手动配置场景和与定义的模拟相关的其他细节。启动 mock api server 后,就可以启动localhost:30020/mocking了。如果我们希望禁用所有模拟场景,则可以从 UI 中选择 All to passThrough 选项,并且调用将转到实际的 REST 后端应用服务器。
{
"/api/*": {
"target": "http://localhost:30020",
"secure": false,
"logLevel": "debug"
},
"/application-server/*": {
"target": "http://localhost:30020",
"secure": false,
"logLevel": "debug"
},
"/ngapimock/*": {
"target": "http://localhost:30020",
"secure": false,
"logLevel": "debug"
},
"/mocking/*": {
"target": "http://localhost:30020",
"secure": false,
"logLevel": "debug"
}
}
(((注:我们的Dev App Server一般运行在30020上))
量角器配置
在 protractor.conf.js 中添加了与 Angular 版本、量角器插件包和使用自定义全局 ngApiMock 客户端名称(作为 API 方法调用的 e2e 规范中使用的唯一名称声明)相关的 ngApiMock 相关选项。
options: {
globalName: 'ngApiMockClient', //this name should be used for declaration of Client in e2e tests
}
以下选项已删除:
useAllAngular2AppRoots → 删除以避免与 ng-apimock 量角器选项附带的 Angular 版本规范冲突。
baseUrl → 已删除以避免与下一步中描述的代理配置冲突。
angular.json 更改
添加新的 Webpack DevServer 目标 serve-e2e 以指向 mock-proxy.conf.json。然后在典型的 E2E 测试运行期间调用此目标以代替常规的“服务”目标以启动应用程序。这个新的目标添加确保我们不使用代理配置来触发在开发过程中经常使用的“ng serve”触发的一般应用程序启动。
"serve": {
"builder": "@angular-devkit/build-angular:dev-server",
"options": {
"browserTarget": "tnps-portal:build"
},
"configurations": {
"production": {
"browserTarget": "tnps-portal:build:production"
}
}
},
"serve-e2e": {
"builder": "@angular-devkit/build-angular:dev-server",
"options": {
"browserTarget": "tnps-portal:build",
"proxyConfig": "mockapi/mock-proxy.conf.json"
},
"configurations": {
"production": {
"browserTarget": "tnps-portal:build:production"
}
}
},
并将 serve-e2e 指定为 e2e devServer 目标...
"e2e": {
"builder": "@angular-devkit/build-angular:protractor",
"options": {
"protractorConfig": "e2e/protractor.conf.js",
"devServerTarget": "tnps-portal:serve-e2e"
},
"configurations": {
"production": {
"devServerTarget": "tnps-portal:serve:production"
端到端使用
在启动 mock api 服务器之前,应将所有 mock 文件声明为 *.mock.json 并预设为 *.preset.json,以处理所有 mock 和预设。可以通过在 mock-server.js 文件中指定模拟和预设的正则表达式模式来修改此默认配置,例如可以将其设置为 *Mock.json 和 *Preset.json
import { Client } from '@ng-apimock/base-client';
declare const ngApiMockClient: Client; // !IMPORTANT: the name of the constant should match the global name defined in protractor.conf.js
it('sample mock test', () => {
ngApiMockClient.selectScenario('sample-mock-name', 'sample-scenario-name',);// sample-mock-name is the name of the mock, and sample-scenario-name is the response scenario name as defined in some sample.mock.json
上面的代码应该为特定的模拟选择场景,这基本上意味着可以为一些特定的用例返回一些特定的数据。该数据也在 sample.mock.json 中的响应下定义如下 -
"name": "sample-mock-name",
"isArray": true,
"request": {
"url": "application-server/actual-endpoint-name", // Endpoint Url for the Mock Request
"method": "GET" // HTTP call type - GET, POST, etc.
},
"responses": {
"fileDataScenario": {
"file": "../data/sampleData.json", // returns json Array data from a file
"default": false
},
"emptyListScenario": {
"data": [{}], // returns data as array, "isArray" : true mandatory for the same mock.
"default": true // this scenario's data will be returned if no scenario is selected from E2E Code or /mocking UI.
}
选择场景足以测试简单的用例。对于更复杂的情况,您需要确保在运行时从某些特定模拟返回特定场景,请配置如下预设-
{
"name": "sample-preset",
"mocks": {
"sample-mock-name": {
"scenario": "fileDataScenario",
"delay": 3000
},
"sample-mock-name-2": {
"scenario": "someOtherScenarioFromMock2",
"delay": 3000
},
"sample-mock-name-3": {
"scenario": "oneMoreScenarioFromMock3",
"delay": 3000
}
},
"variables": {
"something": "awesome"
}
}
在 e2e 规范中
ngApiMockClient.selectPreset('sample-preset');
上面的代码块描述了一些常见的示例,这些示例可能对使用量角器和 ng-apimock 模拟 E2E 测试的 REST 调用很有用。
ng-apimock 文档