O'Reilly logo

Hands-On Design Patterns with C# and .NET Core by Jeffrey Chilberto, Gaurav Aroraa

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Unit tests

To illustrate the issues that the singleton pattern addresses, let's start with a simple unit test that adds 30 books to the repository, updates the quantity of the different books, and then verifies the result. The following code shows the overall unit test, and we will explain each step individually:

 [TestClass]public class InventoryContextTests{     [TestMethod]    public void MaintainBooks_Successful()    {         var context = new InventoryContext();        // add thirty books        ...        // let's update the quantity of the books by adding 1, 2, 3, 4, 5 ...        ...        // let's update the quantity of the books by subtracting 1, 2, 3, 4, 5 ...        ...        // all quantities should be 0        ...    } }

To add 30 books, the context instance is used to add books from Book_1 to 

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required