Warm tip: hotheads can be pulled directly to the last view method steps.

What is a mock?

A mock is a method used during development to create a mock object that is not easy to construct or retrieve.

What are the benefits of using mocks?

1. Teams can work better concurrently

Concurrent? That’s right, technical people should use the word concurrency. Use the mock, can no longer need to wait for each other between the teams progress, only need to agree on each other’s data specification (document), you can use a mock build a usable interface, can be faster to develop and debug and self-test, promote progress at the same time, will also find the timing of the defects in advance greatly.

2. Enable the test-driven Development (TDD) mode, the legendary test-driven Development

Unit testing is a common practice in TDD. Imagine if you are testing and some collaborative modules are not completed. Mocks are a great way to solve this problem. Once the interface is defined, the tester can create a mock to add the interface to the automated test environment and create the test ahead of time.

3. Test coverage

Such as an interface in a variety of different conditions to return different values, our traditional practice is repetition in this state and then to request interface, which is very unscientific, and the retrieval method is very possible because of the timing of the operation or improper operation failure, even before the pollution data in the database. If we use mocks, we don’t have to worry about these problems at all.

4. Easy to demonstrate

By using mock to simulate the data interface, we can demonstrate the product without the need for server-side development without developing only the UI.

5. Isolate the system

To avoid database contamination, mock interfaces can be used to keep the database clean.

How to use mocks?

Mock has a number of online platforms, almost all of which require registration and are unstable. So longyoung, I came up with an idea: can I build a mock locally, which is convenient and quick? Isn’t it nice to be able to work without Internet access? In the Internet to find a circle, found that there is no relatively simple method, and then through efforts, finally achieved. The method is very simple, so it is not particularly intelligent, later can think of ways to upgrade the method. Cut the crap and get straight to the steps:

1. Install Node.js.

2. Create a file in a directory to store JSON data, for example, test.json.

3. Run the CD command to switch to the file directory (for example, D:\gittest\test) and run the http-server -p 5050 command. -p indicates the specified port. Remember to add a port number when accessing the port.

4. Access methods: http://192.168.3.252:5050/test.json. IP + port + file name (including suffix).

PS: This article was originally posted on the wechat public account Longyoung. Follow and reply to “51 Ebook” to get the classic ebook for free for programmers.