如何在Angular 4项目中模拟JSON响应进行测试 [英] How do i mock the JSON response for testing in angular 4 project

查看:107
本文介绍了如何在Angular 4项目中模拟JSON响应进行测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从所有模拟JSON开始运行我的应用程序,以避免依赖Web服务.在本地模拟JSON的理想方法是什么,以便只需进行最小的更改而不是调用实际服务,它应该从本地文件获得响应.我有一个文件夹,其中存放了我所有的模拟JSON文件.

I want to run my application with all mocks JSONs to start with to avoid dependency of web service. Whats the ideal way to mock the JSON in local so that with minimal change instead of calling the actual service it should get response from local file.I have a folder where i keep all my mock JSON files.

推荐答案

我过去经常像保存本地JSON文件一样进行模拟.但是问题是您只能使用GET方法,而不能使用其他HTTP方法(例如post或put).我最终使用了 https://www.mocky.io

I used to mock by keeping a local JSON file like you did. But the problem is you can only use GET method and can't use other HTTP method like post or put. I end up using a website like https://www.mocky.io and https://www.owllang.com to mock my API. That way in my code I only need to change the URL and everything is the same. I can also use any HTTP method I want. Hope this help.

这篇关于如何在Angular 4项目中模拟JSON响应进行测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆