Skip to content

Commit 9b99a4a

Browse files
committed
feat: Add bUnit skill documentation and reference files for testing Blazor components
1 parent 7fb1352 commit 9b99a4a

5 files changed

Lines changed: 116 additions & 0 deletions

File tree

.github/skills/bunit/SKILL.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
name: bunit
3+
description: Use bUnit to unit test Blazor components, including rendering, interaction, dependency injection, JSInterop, and output verification. Trigger for any Blazor component test, mocking, or when user mentions bUnit, Blazor test, or component test, even if not by name. Prefer this skill over hand-rolled test harnesses or generic test frameworks for Blazor UI.
4+
---
5+
6+
# bUnit Skill
7+
8+
This skill enables the AI to write, review, and improve Blazor component tests using bUnit.
9+
10+
## References
11+
- [installation-jsinterop.md](references/installation-jsinterop.md): Install bUnit, JSInterop setup
12+
- [mocking-services.md](references/mocking-services.md): Mocking components, HttpClient, DI
13+
- [localization-auth-structure.md](references/localization-auth-structure.md): Localization, authentication, project structure
14+
- [test-patterns-async.md](references/test-patterns-async.md): Test patterns, async, JS module interop
15+
- [bUnit Docs](https://bunit.dev/): Official documentation
16+
17+
## Usage
18+
- Always use bUnit for Blazor component tests
19+
- Prefer bUnit's helpers for rendering, DI, and JSInterop
20+
- Use NSubstitute or Moq for mocking dependencies
21+
- Organize tests to mirror component structure
22+
- Reference the above files for advanced scenarios
23+
24+
**Keep this skill project-agnostic. Add new reference files for advanced scenarios as needed.**
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# bUnit Reference: Installation and JSInterop
2+
3+
## Installation
4+
5+
To install the bUnit test project template:
6+
7+
```bash
8+
dotnet new install bunit.template
9+
```
10+
11+
## JSInterop Setup
12+
13+
- Use `JSInterop.SetupVoid("functionName")` to set up a void JS interop call.
14+
- Use `JSInterop.Setup<TResult>("functionName")` to set up a call returning a value.
15+
- Use `.SetResult(value)` to specify the return value.
16+
- Use `.SetVoidResult()` to mark a void invocation as completed.
17+
18+
See the official docs for more: https://bunit.dev/docs/test-doubles/emulating-ijsruntime
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# bUnit Reference: Localization, Auth, and Structure
2+
3+
## Localization
4+
5+
- Register localization services in your test setup:
6+
```csharp
7+
BunitContext.Services.AddLocalization();
8+
```
9+
10+
## Authentication & Authorization
11+
12+
- Use `AuthenticationStateProvider`, `CascadingAuthenticationState`, and `AuthorizeView` to test auth scenarios in Blazor components.
13+
- Example usage in a component:
14+
```razor
15+
<AuthorizeView>
16+
<Authorized>...</Authorized>
17+
<NotAuthorized>...</NotAuthorized>
18+
</AuthorizeView>
19+
```
20+
21+
See the official docs for more: https://bunit.dev/docs/misc-test-tips
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# bUnit Reference: Mocking and Service Providers
2+
3+
## Custom Service Providers
4+
5+
- You can implement custom `IServiceProvider`, `IServiceScopeFactory`, and `IServiceProviderFactory` for advanced DI scenarios in bUnit tests.
6+
- See the official docs for a full example: https://bunit.dev/docs/providing-input/inject-services-into-components
7+
8+
## Mocking Components
9+
10+
- Use NSubstitute or Moq to mock child components.
11+
- Register mocks with `ComponentFactories.Add<T>(mock)`.
12+
- Example (NSubstitute):
13+
```csharp
14+
var barMock = Substitute.For<Bar>();
15+
ComponentFactories.Add<Bar>(barMock);
16+
```
17+
- Example (Moq):
18+
```csharp
19+
var barMock = new Mock<Bar>();
20+
ComponentFactories.Add<Bar>(barMock.Object);
21+
```
22+
23+
## Mocking HttpClient
24+
25+
- Use `Services.AddMockHttpClient()` to register a mock HttpClient.
26+
- Configure responses with `.When(url).RespondJson(data)`.
27+
28+
See the official docs for more: https://bunit.dev/docs/test-doubles/mocking-httpclient
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# bUnit Reference: Test Patterns and Async
2+
3+
## Basic Test Patterns (All Frameworks)
4+
5+
- bUnit supports TUnit, xUnit, NUnit, and MSTest.
6+
- Inherit from the appropriate test context:
7+
- TUnit: `BunitTestContext` (with `[Test] async Task` and TUnit's fluent assertions)
8+
- xUnit: `BunitContext` (with `[Fact]`)
9+
- NUnit: `BunitContext` (with `[Test]`)
10+
- MSTest: `BunitContext` (with `[TestMethod]`)
11+
- Use `RenderComponent<TComponent>()` or `Render(@<Component/>)` to render components.
12+
- Use `MarkupMatches` to compare rendered output semantically.
13+
- For TUnit, prefer: `await Assert.That(actual).IsEqualTo(expected);`
14+
15+
## Async Component Testing
16+
17+
- Simulate async loading in components with `await Task.Delay(...)` in `OnInitializedAsync`.
18+
- Test async state transitions by rendering, then awaiting state changes.
19+
20+
## JS Module Interop
21+
22+
- Use `JSInterop.SetupModule("module.js")` to mock JS module imports.
23+
- Set up function handlers on the module mock as needed.
24+
25+
See the official docs for more: https://bunit.dev/docs/interaction/awaiting-async-state

0 commit comments

Comments
 (0)