What Is Mock and Specific Scenarios for Using Mock
Mock is a testing technique used in software development to mock up a fake implementation of a feature for testing and debugging while the actual code is still unimplemented.
In software development, Mock refers to creating an object to simulate a real object. Why do we need to simulate a real object instead of using it? Because some real objects are not easy to construct or obtain. For example:
- The real API is still under development, but front-end development depends on it. In this case, we can use Mock to simulate the API's return results.
- In the development environment, it is impossible to access the third-party service that is dependent. In this case, we can use Mock to simulate the third-party service's return results.
Why use Mock
Using Mock helps to improve software quality and development efficiency.
Improve software quality
To develop high-quality software, we need to do functional testing. Functional testing not only focuses on the normal use of software but also needs to pay attention to various abnormal situations. Abnormal situations are not easy to reproduce stably and require Mock. Common abnormal situations include unstable network, network timeout, database connection timeout, server malfunction, and API returns exception.
To develop high-quality software, we also need to do performance testing. We use Mock to understand the performance of the software in high concurrency, large data volume, and other scenarios.
To develop high-quality software, we also need to do security testing. In security testing, we mainly focus on the vulnerabilities and weaknesses of the software. We use Mock to detect the security of software by simulating various attack and vulnerability scenarios.
Improve Software Development Efficiency
By mocking external dependencies, you can save time without waiting for the completion of external dependencies. Common scenarios including frontend development use Mock API when the API is not ready.
By mocking the preconditions, you can reduce the time to construct real preconditions. For example, there are three steps in total in the form, and each step depends on the form data filled in the previous step. Currently, in the development of the third step, we can use Mock data filled in the first two steps, and we do not need to manually fill in the data for the first two steps every time.
Specific Scenarios for Using Mock
From the above, we know that the scope of using Mock is very wide. To avoid being too verbose, here will take the Mock API interface as an example to introduce several specific scenarios:
- The API is still under development, but we need to test it. In this case, we can use Mock to simulate the API's return results.
- When the number of calls to a certain API is limited, we can use Mock to simulate the API's behavior so that we can call the API an unlimited number of times in the test.
- When the data structure returned by a certain API is very complicated, we can use Mock to generate fake data to test whether our code can handle the data returned by the API correctly.
- When the behavior of a certain API is affected by external factors, such as an unstable network, server malfunction, etc., we can use Mock to simulate these situations so that our code can handle these abnormal situations correctly.
How to Use Mock
Using Mock requires writing some code. For example, to the Mock API interface, we need to intercept API requests and replace the real API with the Mock API. The core code is as follows:
// Store the real xhr constructor. It can be restored when Mock is not needed.
window.realXhr = window.realXhr || XMLHttpRequest
// Override the XMLHttpRequest constructor.
window.XMLHttpRequest = function () {
var xhr = new window.realXhr()
this.xhr = xhr
for (var attr in xhr) {
if (xhr[attr] instanceof Function) { // Take over xhr functions
// interceptFn implements the logic of Mock.
this[attr] = interceptFn(attr, config)
} else {
Object.defineProperty(this, attr, { // Take over xhr attributes and events
get: getterFactory(attr, config),
set: setterFactory(attr, config),
enumerable: true
})
}
}
}
Using tools can make mocking easier, and Apidog is recommended for mocking APIs. Apidog's mocking feature is powerful and allows for easy toggling of whether to use mocking. Additionally, it can automatically generate mock content based on the definition of the interface response. Give it a try!