
bUnit is the standard testing library for Blazor components. It lets you render components in isolation, interact with them, and assert on their output.
// Install:
// dotnet add package bunit
// dotnet add package xunit
// Test class:
public class CounterTests : TestContext
{
[Fact]
public void Counter_IncrementsOnButtonClick()
{
// Arrange
var cut = RenderComponent<Counter>();
// Act
cut.Find("button").Click();
// Assert
cut.Find("p").TextContent
.MarkupMatches("Current count: 1");
}
}var cut = RenderComponent<ProductCard>(parameters => parameters
.Add(p => p.Product, new Product { Name = "Laptop", Price = 999 }));Services.AddSingleton<IProductService>(mockService);
var cut = RenderComponent<ProductList>();Reference:
TaskLoco™ — The Sticky Note GOAT