沙雕与大婶 | Mock调你的外部依赖吧
EvanLeung 人气:1
>**故事背景:**
>沙雕在公司负责API项目的开发,很认真负责,经常加班加点赶进度,却常常被老板吐槽说他开发效率太低,他自己也很委屈,因为他所负责的项目常常依赖大量外部系统,他只好等对方开发完才一个个对接,开发时间也很难把握,导致效率非常低,团队里的测试同学也很无奈,只能等他开发完才能测试。有一天,他咨询公司里一位大婶,寻求解决方案。
![](https://img-blog.csdnimg.cn/20200305215155615.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0V2YW5fTGV1bmc=,size_16,color_FFFFFF,t_70)
# **大婶演示**
假如你依赖下游系统一个API,它的Endpoint是```GET http://xxxx/accounts```
## **配置你的Mock Server**
1.创建你的专属```Mock Server```
![](https://img-blog.csdnimg.cn/20200305220348923.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0V2YW5fTGV1bmc=,size_16,color_FFFFFF,t_70)
2.定义你依赖的下游系统一系列API Endpoint,并且填入你想要的响应数据
Requet Path:
```/accounts```
Request Boday:
```json
{
"code": 200,
"message": "Operation Successfully",
"data": [
123456,
654321
]
}
```
![](https://img-blog.csdnimg.cn/20200305221306932.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0V2YW5fTGV1bmc=,size_16,color_FFFFFF,t_70)
3.填入Mock Server名称,点击创建
![](https://img-blog.csdnimg.cn/20200305221627398.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0V2YW5fTGV1bmc=,size_16,color_FFFFFF,t_70)
![](https://img-blog.csdnimg.cn/20200305221754485.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0V2YW5fTGV1bmc=,size_16,color_FFFFFF,t_70)
这就已经创建完成了
4.获取Mock Server自动生成的域名
![](https://img-blog.csdnimg.cn/20200305222202894.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0V2YW5fTGV1bmc=,size_16,color_FFFFFF,t_70)
![](https://img-blog.csdnimg.cn/20200305222322328.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0V2YW5fTGV1bmc=,size_16,color_FFFFFF,t_70)![](https://img-blog.csdnimg.cn/20200305222438123.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0V2YW5fTGV1bmc=,size_16,color_FFFFFF,t_70)
```https://36669d4e-1c55-448c-a380-c0a71d8b0ece.mock.pstmn.io```这就是我们Mock Server自动生成的域名,后面我们只需要用该域名请求即可
5.用浏览器代替你的应用,尝试通过Mock Server访问我们定义的API Endpoint
![](https://img-blog.csdnimg.cn/20200305222728261.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0V2YW5fTGV1bmc=,size_16,color_FFFFFF,t_70)
这样就成功啦,so easy!
![](https://img-blog.csdnimg.cn/20200305223432166.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0V2YW5fTGV1bmc=,size_16,color_FFFFFF,t_70)
***
# **参考链接**
**官方文档**:[Mock Server配置](https://learning.postman.comhttps://img.qb5200.com/download-x/docs/postman/mock-servers/setting-up-mock/)
加载全部内容