ada-blazor-bunit-testing

Write bUnit tests for Blazor components using verified rendering, event, and assertion patterns.

Updated Jul 23, 2026
One-click install
npx skills add https://github.com/wubing7755/Ada --skill ada-blazor-bunit-testing-wubing7755
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: ada-blazor-bunit-testing
Source: https://github.com/wubing7755/Ada/tree/main/skills/software-development/ada-blazor-bunit-testing
Command: npx skills add https://github.com/wubing7755/Ada --skill ada-blazor-bunit-testing-wubing7755

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Writing bUnit tests for Blazor components often fails in confusing ways: EditorRequired parameters silently stay null, Assert.Single reports misleading collection counts, Click() failures surface late as UnknownEventHandlerIdException, and optional [Inject] properties crash on .NET 6. This Skill provides verified bUnit 1.x behaviors and robust test patterns so tests fail at the right assertion for the right reason. ## Core Features & Use Cases - Verified bUnit 1.x facts: Documents real observed behaviors of RenderComponent, WaitForAssertion, cut.Render(), Click(), UploadFiles, and KeepAliveWithinGroup lifetime modes. - Robust test patterns: Prescribes re-querying FindAll before clicks, flushing pending renders with cut.Render(), asserting exact element counts, and containing async handler exceptions with visible status messages. - Diagnostic workflow: Provides a fail-first repro workflow and a throwaway dump-state test pattern using ITestOutputHelper to resolve ambiguous failures in one cycle. - Use Case: A bUnit test fails with "Collection contained 2 items" on Assert.Single even though the flow looks correct; the Skill diagnoses the selector-count trap and prescribes asserting the exact expected count before clicking the intended toolbar button. ## Quick Start Ask the agent to write or fix a bUnit test for a Blazor component, for example a test that renders a component, uploads a file through InputFile, and asserts the resulting DOM state.

Frequently Asked Questions about ada-blazor-bunit-testing

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I write bUnit tests for Blazor components?

Use RenderComponent to mount the component, WaitForAssertion to await asynchronous renders, and FindAll with Click to trigger events. Re-query elements immediately before each click and flush pending renders with cut.Render() when fire-and-forget StateHasChanged calls are involved.

Why does bUnit Assert.Single fail with Collection contained 2 items?

The selector matches more elements than expected, so Assert.Single fails inside WaitForAssertion with a misleading message. Assert the exact expected count first, then index into FindAll results to click the intended element.

Why does bUnit Click throw UnknownEventHandlerIdException later?

Click() is fire-and-forget, so a pending re-render can replace the DOM between the FindAll query and dispatch, invalidating the event handler id. The failure surfaces in the next WaitForAssertion; fix it by flushing renders with cut.Render() and re-querying before clicking.

Does bUnit enforce EditorRequired parameters on RenderComponent?

No, bUnit 1.x renders components with missing EditorRequired parameters without throwing, leaving them null. Razor route usage is the opposite: omitting the parameter emits RZ2012, which becomes an error under TreatWarningsAsErrors.

Can Blazor components use optional [Inject] properties on .NET 6?

No, .NET 6's ComponentFactory throws for any unregistered [Inject] property, including nullable types, in both tests and real apps. Inject IServiceProvider instead and resolve the optional service with GetService, falling back to a default implementation.

How do I test InputFile upload validation in bUnit?

Call UploadFiles with InputFileContent.CreateFromText on the FindComponent<InputFile>() result, then re-query the component before subsequent uploads since it re-renders. Assert the visible status message and that the underlying store remains unchanged.