Examples of approaches to unit testing different kinds of code in C#. These examples start with simple cases and move on to more complex patterns.
If you like or are using this project to learn, please give it a star to help others find it. Thanks!
The simplest kinds of unit tests create collaborators, call methods, and inspect the results. These work best with public methods with non-void return signatures and no side effects.
Tests should follow SOLID principles, including Single Responsibility. Grouping tests by the method being tested helps achieve this. Read the following naming convention for a proven approach to organizing and naming your tests. Also, the FizzBuzz and String Calculator katas are great to use when first learning state-based testing.
When you can't evaluate whether a method did the right thing by looking at the state it returns or modifies publicly, another approach is to test the behavior of the method itself. This is often called "behavior-based" testing and frequently uses mock objects (or spies or fakes or other techniques) to capture behavior that took place within the method being tested. Collectively these different techniques are known as test doubles.
Confirming the state of private or internal values when testing from another class and assembly can be a challenge. There are some patterns that can be used to overcome these challenges in most cases.