使用server.js和proxy.config.json使用Protractor模拟服务 [英] Mock services using server.js and proxy.config.json using Protractor
问题描述
我正在尝试使用server.js和proxy.config.json文件来模拟服务,以便可以在量角器测试中使用该模拟服务.我要使用的方法是拥有这样的测试文件:
I am trying to mock service using server.js and the proxy.config.json file so I can use that mocked service in a Protractor test. The way I'm going about it is having the test file like this:
const server = require('server');
const { get, post } = server.router;
const { json } = server.reply;
server({ port: 3000 }, [
get('/abc', ctx => {
return json({
foo: "bar"
})
})
]);
以及类似的proxy.config.json文件:
and the proxy.config.json file like this:
{
"/xyz": {
"target": "http://localhost:3000/abc",
"changeOrigin": true,
"secure": false,
"logLevel": "debug",
"pathRewrite": {
"^/xyz": ""
}
}
}
然后我打电话给我进行测试
Then I run my tests by calling
ng e2e --proxy-config proxy.config.json
但是,我没有成功,因为我的应用程序在例如 http://localhost:49156 中运行并且我要模拟的服务在 https://localhost:8443/xyz 中运行.如果我想模拟和代理与应用程序相同的端口/协议(49156和HTTP)中的某些内容,我可以这样做,但是对于在端口8443和https中运行的服务则无法.有人可以帮我弄这个吗?我究竟做错了什么? 谢谢您的时间.
However, I am not being successful because my application runs, for example, in http://localhost:49156 and the service I wish to mock runs in https://localhost:8443/xyz. If I wish to mock and proxy something coming from the same port/protocol as the application (49156 and HTTP) I am able to do so, however, for the service running in port 8443 and https I am not able to. Can someone help me with this? What am I doing wrong? Thank you for your time.
推荐答案
我终于可以通过创建如下的environmentE2E.ts文件来解决最初的问题:
I was finally able to solve my initial issue by creating an environmentE2E.ts file as such:
export const environment = {
production: false,
baseURL: 'http://localhost:49156'
};
在angular-cli.json文件中添加以下内容:
adding the following in angular-cli.json file:
"environments": {
"dev": "environments/environment.ts",
"prod": "environments/environment.prod.ts",
"e2e": "environments/environmentE2E.ts" // Added only this line, the others were already there
}
并使用以下命令调用测试:
and called the tests with the following command:
ng e2e --proxy-config proxy.config.json --environment e2e
希望它对其他人有帮助. 谢谢.
Hope it helps others. Thank you.
这篇关于使用server.js和proxy.config.json使用Protractor模拟服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!