This Blazor article shows how to unit test a Blazor component. You can find the Github source repository this article series is based on here.
In this article, I will present a way to test the component we have been writing in this previous article: Write a reusable Blazor component. I consider at this stage that we are now familiar with creating a reusable Blazor component and we will focus on how to unit test it.
In order to test our “Component1”, we need to set up a unit test project. Please note that I suppose that we are in the same solution as the component itself. In this article I will use xUnit as unit testing framework and Moq as mocking library but you can use whatever equivalent librairies you prefer. …
You can find the Github source repository this article series in base on here.
To create a reusable component, we need to create a Razor Class Library with visual studio:
Or razorclasslib with dotnet command line interface:
dotnet new razorclasslib
By default, the created project contains several elements:
A predefined “Component1” that is a basic component displaying some styled text in Component1.razor.