Skip to content

Commit a219a87

Browse files
linkdotnetegil
authored andcommitted
docs: Added documentation for generators
1 parent 6142684 commit a219a87

3 files changed

Lines changed: 97 additions & 0 deletions

File tree

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
---
2+
uid: bunit-generators
3+
title: bUnit Generators
4+
---
5+
6+
# bUnit Generators
7+
8+
The `bunit.generators` package contains a set of source generators that can be used to generate code likes stubs for Blazor components. The generators are designed to be used with the [bUnit](https://github.com/bunit-dev/bunit) testing framework for Blazor components. To use the generators, you must install the `bunit.generators` NuGet package in test project.
9+
10+
This page will describe the generators and their usage.
11+
12+
## Component stub generator via `AddStub`
13+
14+
This generator adds the ability to automatically generate stubs for a given type with no setup involved. The generator sits on top of the already
15+
present `AddStub` method.
16+
This comes in handy, when dealing with 3rd party components that might need an extensive setup. Here a small example:
17+
18+
Given the following component
19+
```razor
20+
<ThirdPartyText Text="@count" />
21+
<button @onclick="IncrementCount">Increase by one</button>
22+
@code {
23+
private int count;
24+
25+
private void IncrementCount()
26+
{
27+
count++;
28+
}
29+
}
30+
```
31+
32+
If `ThirdPartyText` is a 3rd party component, that needs a lot of setup, it might be easier to just stub it out:
33+
34+
```csharp
35+
[Fact]
36+
public void Text_button_gets_initial_count()
37+
{
38+
// This call will automatically generate a stub for the ThirdPartyButton component
39+
// with the name "ThirdPartyButtonStub"
40+
ComponentFactories.AddStub<ThirdPartyText>();
41+
var cut = Render<Counter>(@<Counter />);
42+
43+
cut.Find("button").Click();
44+
45+
// Retrieves the stub from the render tree and checks if the text is "1"
46+
cut.FindComponent<ThirdPartyTextStub>().Instance.Text.Should().Be("1");
47+
}
48+
```
49+
50+
### Setup
51+
To use the generator, the **Interceptor** feature has to be used inside the csproj file:
52+
53+
```xml
54+
<Project Sdk="Microsoft.NET.Sdk">
55+
<PropertyGroup>
56+
<TargetFramework>net8.0</TargetFramework>
57+
<!-- This line is required to enable the generator and interceptor -->
58+
<InterceptorsPreviewNamespaces>$(InterceptorsPreviewNamespaces);Bunit</InterceptorsPreviewNamespaces>
59+
```
60+
61+
Due to the usage of **Interceptors** the generator is only available for .NET 8.0 and above. The generator does create a `partial` class, so it can be extended with custom logic if needed.
62+
63+
## Component stub generator via `StubAttribute`
64+
65+
This generator adds the ability to automatically generate stubs for a given type via an attribute.
66+
The general setup for the given component above looks like this:
67+
```csharp
68+
namespace MyTest;
69+
70+
public class FeatureTests : TestContext
71+
{
72+
[Fact]
73+
public void Test()
74+
{
75+
ComponentFactories.Add<ThirdPartyText, ThirdPartyStub>();
76+
...
77+
}
78+
}
79+
80+
[Stub(typeof(ThirdPartyText))]
81+
internal partial class ThidPartyStub { }
82+
```
83+
84+
Current limitations of this approach is that he stubbed type is not allowed to be nested inside the test class.

docs/site/docs/extensions/index.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
uid: extensions
3+
title: Extensions for bUnit
4+
---
5+
6+
# Extensions for bUnit
7+
8+
This section covers the various extensions available for bUnit. These extensions are not part of the core bUnit package, but are instead available as separate NuGet packages. The extensions are listed below, and each has its own documentation page.
9+
10+
* **[bunit.generators](xref:bunit-generators)** - A set of source generators that can be used to generate code like stubs for Blazor components

docs/site/docs/toc.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
## [Faking IWebAssemblyHostEnvironment](xref:fake-webassemblyhostenvironment)
3131
## [Testing with InputFile component](xref:input-file)
3232

33+
# [Extensions][xref:extensions]
34+
## [bunit.generators](xref:bunit-generators)
35+
3336
# [Miscellaneous testing tips](xref:misc-test-tips)
3437
# [External resources](xref:external-resources)
3538
# [Contribute](xref:contribute)

0 commit comments

Comments
 (0)