Because of the high complexity and depth of this episode, we will share with you the episode outline and the topics discussed here as a reference.
What is unit testing?
test for the smallest possible pieces of your program.
كل حاجة فيها سليمة بس لوحدها - سلطان السكري
Why Unit Testing
Unit Testing Best Practices
1- Identifying Units:
When it comes to the unit you are testing, you need to answer three questions.
a. What is the output of this unit?
b. What helper functions that this unit is using to achieve that output?
c. Are there any side effects resulting from this unit. (it modifies state variables outside its scope).
2- Naming & Description
Why? it makes the test easier to read and hence easier to figure out what went wrong.
Describe & It. (This thing / should or does something)
- The search function should return an array of strings matching the search keyword.
- The search function should return an empty array when a keyword is not matching any.
- throw an exception if the keyword is an empty string.
3- Mocking & Stubs
- The unit test is 50% mocking and 50% clean code.
Check the resources section.
Episode Picks: