Skip to content

Automatic Unit Test Implementation for Network Calls

Abdul Malik edited this page Sep 3, 2022 · 2 revisions

🛠️ Automatic Unit/Widget Testing

The implementation as well as the file structure for the tests will be similar to the guidelines of the stacked architecture. For more info on testing guidelines click here.

324444444

To stub network calls we will create an extension class to the MockIOClient in the mock_ioclient.data file and override the post() and get() calls of this class. This will enable us to return the required test data response since we are not making actual network calls.

Once we override these functions we can use a simple switch statement to return the correct data depending upon the call arguments.

Steps for adding a test that requires a network call to be stubbed

  1. Add the hardcoded data of the body that is sent (incase of a POST call) and the response from API in the test_data.dart file.
  2. In the mock_ioclient.dart file, make sure the POST/GET call from the function returns the data from Step 1 using appropriate checks.
  3. Add the test in the api_service_test.dart file