Dojo to introduce unit tests in C# with NUnit and Moq
This small project aims to discover unit tests in C# with NUnit and Moq.
-
git clone the project
-
Open the solution in Visual Studio
The latest version of Part 1 is step9: 2 classes, 1 test class, 100% covered.
The latest version of Part 2 is step13: 4 classes, 1 test class. ShoppingItem is 100% covered, we should now test class InventoryService.
You can run the tests.
You can also checkout the different steps to run the dojo from beginning:
In this introduction, we will learn step by step how to write unit tests (test after).
The project contains only 2 classes: ShoppingBasket and ShoppingItem.
ShoppingItem is a very simple class with 4 properties.
ShoppingBasket contains some methods to get the number of items in the basket, to add an item into the basket, etc.
We will add unit tests step by step to cover this class.
In order to introduce the concept of mocks (test double), we add a feature in our simple application: the basket will now ask to a InventoryService if the item is available or not before adding the item into the basket.
This InventoryService will access a database (not implemented for the example) so that the unit tests written into Part 1 are failing. Then We realize that it becomes very hard to test. We then introduce the concept of mock (with Moq).
We make the tests passed, and add some more feature that again access the database, and test them.