Once the creation of Create command for an object was finished, I continued on building the unit test. Unit testing is an important part of software development because it helps to ensure that the code is working correctly and meets the requirements of the system we’re building.
Unit testing is also crucial for maintaining the quality of code over time. It allows developers to test new features or changes made to the system and detect any regression bugs or unexpected side effects.
To test the Create command, I started with the simplest test. Firstly, in the unit test, I created a test environment for the application to run in. Then, I initialized a set of data by creating an array of entity objects and added them to the database using the AddRange() method then called SaveChanges() method to save the changes to the database.
Next, I created a new test method that simulated the creation of a new object by sending a POST request to the object controller API endpoint with a CreateCommand object containing the necessary details. The test method checked whether the response status code was OK, which confirmed that the object was successfully created. It also checked whether the created object ID was not equal to 0, which confirmed that a new object was indeed created. Additionally, the test method checked whether the number of objects in the database increased by one by retrieving the count of objects before and after the creation of the new object using the AsNoTracking method.
To summarize, unit testing plays a critical role in verifying that the code is functioning correctly and meets the system requirements. Moving forward next week, I will continue creating unit tests, to detect any bugs or errors early on. By thoroughly testing the system, I can ensure that it functions as intended and meets the necessary specifications.
