如何模拟 REST API [英] How to mock REST API

查看:57
本文介绍了如何模拟 REST API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 MVC 应用程序,需要调用来自第三方供应商的 REST API.

I have an MVC application that needs to invoke a REST API from a third party vendor.

REST API 应该返回一个 JSON 结果,但在我们开始开发和测试我们的 MVC 应用程序之前它不会准备好,所以我想模拟 REST API(供应商将提供).

The REST API should return a JSON result, but it won't be ready before we start developing and testing our MVC application, so I would like to mock the REST API (that vendors will be providing).

我研究了 MOQ,但它似乎不支持模拟 REST API.

I researched MOQ but it doesn't seem to support mocking of REST API.

推荐答案

最好的方法可能取决于什么需要最少的学习曲线和设置,以及什么适合您的环境.

The best approach probably depends what requires the least learning curve and setup for you and what fits in your environment.

我已经使用node.js和express做了这种事情.这是一个非常快速的启动,并且很容易做到.作为最小的情况,我只是将我需要的 json 放在文件中并为它们提供服务以响应 REST 请求.更改路由以匹配 api 非常容易.我有时对 json 文件很懒惰,所以我用 SQL 编写查询来生成它们,或者如果我需要大量数据,则编写 js 代码来生成它们.

I have done this kind of thing using node.js and express. It was a very quick start up, and easy to do. As a minimal case I just put the json I need in files and serve them in response to REST requests. Changing the routes around to match an api is pretty easy. I am lazy sometimes about json files so I write queries in SQL to produce them, or write js code to produce them if I need a lot of data.

如果您使用的是 ASP.NET MVC(不确定那是您指的 MVC),那么定义路由来创建 REST API 非常好.您可能不需要额外的工具.

If you are using ASP.NET MVC (not sure that's the MVC you meant), it is pretty good as defining routes to make a REST API. You might not need extra tools.

我喜欢真正的服务器和真正的语言,而不是罐头工具.如果您是一名熟悉 Selenium 和 Javascript 的测试人员,那么 Node.js 方法可能是您的最佳选择.您了解这门语言,无需投入太多资金即可启动服务器.

I like a real server and a real language better than a canned tool. If you are a tester familiar with Selenium and Javascript, the Node.js approach might be the best win for you. You know the language, and don't have to invest much to get a server going.

这篇关于如何模拟 REST API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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