|
| 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. |
0 commit comments