Best practices unit testing Blazor components with bUnit
summary
bUnit is a testing library layered on xUnit/NUnit that renders Blazor components with the real renderer but no browser or JS, so tests run in milliseconds and can inspect the AngleSharp DOM and component instances. It provides event triggers, lifecycle emulation, semantic markup comparison (markupMatches with regex/ignore modifiers), and component substitution via factories/Stop<T> plus mocking (Moq) for isolating children. Example: a dynamic data table is refactored into header/row components and verified for composition, markup and data passing; tests remain stable and maintainable.
AI-written summary · about
chapters (17)
- 0:00Intro
- 3:11What is bonit?
- 9:12The Counter component
- 9:30Test of component
- 12:33Running Unit tests
- 13:24Functionality testing - configure pizza dialog
- 16:16To verifying markup, do it semantically!
- 17:36Semantically verifying markup
- 18:29Semantic verification modifiers
- 20:18Creating testable and maintainable components
- 22:05Dynamic component example
- 28:40Creating maintainable tests
- 37:03Summary -testing
- 37:59Isolating the component under test
- 41:54Rules for substitute components
- 43:28Substitution with the built-in Stub component
- 46:39Mocking components
related talks
38mTesting Blazor Application Components: Fun with Benefits
54mHow to create maintainable and testable Blazor components
39mBlazor Stability Testing for Bullet Proof Application
37mTesting Blazor Components
01h 16mTest-Driven Development With Blazor
36mHow to create maintainable and testable Blazor components
59mBlazor-testing from A to Z
51mComponent-based C# and Razor Programming in Blazor
