Writing component unit tests with bUnit
We have seen the basic tests written by default in the bUnit template. In this section, we will start creating our own tests. We’ll write a test for the ModalPopup
component we developed in the Developing templated components section in Chapter 3, Developing Advanced Components in Blazor.
Writing the first component unit test
ModalPopup
is a good starting point for us as it doesn’t have dependencies and it has some parameters, so we can learn how to pass parameters to a CUT.
Let’s get started:
- In the
BooksStore.Tests
project, add theBooksStore
namespaces needed inside_Imports.razor
so we don’t need to reference them in each test file we have:...@using BooksStore.Shared@using BooksStore.Models@using BooksStore.Services
- Create a new Razor component and name it
ModalPopupTests.razor
. - Remove any markup and make the component inherit from the
TestContext
class:@inherits TestContext@code {}
- Inside...