How to mock REST API

随声附和 提交于 2020-06-27 07:26:10

问题


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

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).

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


回答1:


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

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.

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.

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.




回答2:


Here is another REST (and SOAP) mocking tool. https://sourceforge.net/p/soaprest-mocker.

It doesn't have a GUI, instead it's provided with RESTful API and a configuration file. It might be handy for building automated integration tests. You can setup responses and verify requests from your integration test code.




回答3:


I encounter similar problems many times so I decided to start an open source project:

https://github.com/alvinlin123/hmock/




回答4:


I know this is a pretty old question, but for the benefit of people who stumble across it, Mocktastic, provides an excellent downloadable GUI app for MacOS, Linux and Windows, which you can use to mock your APIs. It also allows you to easily share the mocked servers with other members of your team.




回答5:


This can be done with Soap UI, if you have worked with Soap UI already it should be easy. Following are the simple steps:

  • Create a Rest Project in Soap UI
  • Add a new Rest mock to the project
  • Add a mock action
  • Add a mock response to the mock action and run the mock.

I have provided step by step instructions with downloadable examples here




回答6:


You can create a mock of the third-party API online, there are free services like https://mockfirst.com where you can do this.




回答7:


There are lots of tools that allow you to create api mocks or fake apis. It could be desktop apps or web apps. I like online web apps, because it makes your fake api available from any location. For instance I use https://quickmocker.com You may register with one click. Create a project (unique subdomain) and add your mock endpoints. It has a free plan, which should be enough for development. Such online tools apart from allowing to create a fake apis, also allow to intercept requests. It is very convenient for webhooks integration and this quickmocker has a pretty decent one.



来源:https://stackoverflow.com/questions/10344806/how-to-mock-rest-api

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!