ASP.NET Core Unit Tests
Neil Haddley • March 26, 2023
Test driven development
Unit tests are automated tests written and run by software developers to ensure that a section of an application (known as the "unit") meets its design and behaves as intended.
I wanted to add unit tests that could be run as part of a continuous integration pipeline.

$ dotnet test
UnitTest1.cs
TEXT
1using haddley_blazor_api.Server.Models; 2using Microsoft.AspNetCore.Mvc; 3using Microsoft.EntityFrameworkCore; 4 5using haddley_blazor_api.Server.Controllers; 6 7namespace haddley_blazor_app.Tests; 8 9 10[TestClass] 11public class ToDoControllerTests 12{ 13 private ToDoContext? _context; 14 private ToDo? _controller; 15 16 [TestInitialize] 17 public void Initialize() 18 { 19 // Create an in-memory database for testing 20 var options = new DbContextOptionsBuilder<ToDoContext>() 21 .UseInMemoryDatabase(databaseName: "ToDo_TestDatabase") 22 .Options; 23 24 _context = new ToDoContext(options); 25 26 // reset in memory database 27 _context.Database.EnsureDeleted(); 28 29 // Seed the database with test data 30 _context.Items.AddRange(new List<Item> 31 { 32 new Item { Id = 1, Description = "Buy milk", Done = false }, 33 new Item { Id = 2, Description = "Walk the dog", Done = true }, 34 new Item { Id = 3, Description = "Do laundry", Done = false } 35 }); 36 37 _context.SaveChanges(); 38 39 // Create an instance of the controller to be tested 40 _controller = new ToDo(_context); 41 } 42 43 [TestMethod] 44 public async Task GetItems_ReturnsAllItems() 45 { 46 Assert.IsNotNull(_controller); 47 48 // Arrange 49 50 // Act 51 52 var result = await _controller.GetItems(); 53 54 // Assert 55 Assert.IsNotNull(result.Value); 56 Assert.AreEqual(3, result.Value.Count()); 57 } 58 59 60 61 [TestMethod] 62 public async Task GetItem_ReturnsNotFound_ForNonExistentId() 63 { 64 Assert.IsNotNull(_controller); 65 66 // Arrange 67 var nonExistentId = 999; 68 69 // Act 70 var result = await _controller.GetItem(nonExistentId); 71 72 // Assert 73 Assert.IsInstanceOfType(result.Result, typeof(NotFoundResult)); 74 75 } 76 77 78 [TestMethod] 79 public async Task PostItem_AddsNewItem() 80 { 81 Assert.IsNotNull(_controller); 82 Assert.IsNotNull(_context); 83 84 // Arrange 85 var newItem = new Item { Description = "Clean the house", Done = false }; 86 87 // Act 88 var result = await _controller.PostItem(newItem); 89 90 Assert.IsNotNull(result); 91 92 // Assert 93 Assert.IsInstanceOfType(result.Result, typeof(CreatedAtActionResult)); 94 95 // Check that the new item was added to the database 96 Assert.AreEqual(4, _context.Items.Count()); 97 Assert.IsTrue(_context.Items.Any(i => i.Description == newItem.Description)); 98 99 } 100 101 102 [TestMethod] 103 public async Task DeleteItem_RemovesExistingItem() 104 { 105 Assert.IsNotNull(_controller); 106 Assert.IsNotNull(_context); 107 108 // Arrange 109 var existingItem = await _context.Items.FirstOrDefaultAsync(i => i.Description == "Walk the dog"); 110 111 Assert.IsNotNull(existingItem); 112 113 // Act 114 var result = await _controller.DeleteItem(existingItem.Id); 115 116 // Assert 117 Assert.IsInstanceOfType(result, typeof(NoContentResult)); 118 119 // Check that the item was removed from the database 120 Assert.AreEqual(2, _context.Items.Count()); 121 Assert.IsFalse(_context.Items.Any(i => i.Id == existingItem.Id)); 122 } 123 124 125}