bunit.generators 1.37.7

Prefix Reserved
There is a newer prerelease version of this package available.
See the version list below for details.
dotnet add package bunit.generators --version 1.37.7                
NuGet\Install-Package bunit.generators -Version 1.37.7                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="bunit.generators" Version="1.37.7" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add bunit.generators --version 1.37.7                
#r "nuget: bunit.generators, 1.37.7"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install bunit.generators as a Cake Addin
#addin nuget:?package=bunit.generators&version=1.37.7

// Install bunit.generators as a Cake Tool
#tool nuget:?package=bunit.generators&version=1.37.7                

bUnit Generators

This package contains source generators for bUnit, to make it easier and more convenient to write tests.

AddStub Generator

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 present AddStub method. This comes in handy, when dealing with 3rd party components that might need an extensive setup. Here a small example:

Given the following component

<ThirdPartyText Text="@count" />
<button @onclick="IncrementCount">Increase by one</button>
@code {
 private int count;
 
 private void IncrementCount()
 {
	 count++;
 }
}

If ThirdPartyText is a 3rd party component, that needs a lot of setup, it might be easier to just stub it out:

[Fact]
public void Text_button_gets_initial_count()
{
    // This call will automatically generate a stub for the ThirdPartyButton component
    // with the name "ThirdPartyButtonStub"
    ComponentFactories.AddStub<ThirdPartyText>();
    var cut = Render<Counter>(@<Counter />);
    
    cut.Find("button").Click();
    
    // Retrieves the stub from the render tree and checks if the text is "1"
    cut.FindComponent<ThirdPartyTextStub>().Instance.Text.Should().Be("1");
}

Setup

To use the generator, the Interceptor feature has to be used inside the csproj file:

<Project Sdk="Microsoft.NET.Sdk">
	<PropertyGroup>
		<TargetFramework>net8.0</TargetFramework>
		
		<InterceptorsPreviewNamespaces>$(InterceptorsPreviewNamespaces);Bunit</InterceptorsPreviewNamespaces>

This limits the usage to .NET 8 and above.

ComponentStubAttribute

This generator adds the ability to automatically generate stubs for a given type via an attribute. The general setup for the given component above looks like this:

namespace MyTest;

public class FeatureTests : TestContext
{
    [Fact]
    public void Test()
    {
        ComponentFactories.Add<ThirdPartyText, ThirdPartyStub>();
        ...
    }    
}

[ComponentStub<ThirdPartyText>)]
internal partial class ThirdPartyStub { }

Current limitations of this approach:

  • The stubbed type is not allowed to be nested inside the test class.
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories (1)

Showing the top 1 popular GitHub repositories that depend on bunit.generators:

Repository Stars
linkdotnet/Blog
A blog (engine) completely written in C# and Blazor. It aims to be a simple use and easy to extend platform. Blogposts are written in Markdown and are rendered to HTML. This gives all the flexibility needed to express yourself but also have an easy way of creating posts in the first place.
Version Downloads Last updated
2.0.33-preview 129 7/21/2024
2.0.24-preview 1,508 3/13/2024
2.0.22-preview 97 3/9/2024
1.37.7 131 12/13/2024
1.36.0 128 11/12/2024
1.35.3 82 11/12/2024
1.33.1-preview 67 10/7/2024
1.31.1-preview 131 8/14/2024
1.28.4-preview 109 3/17/2024
1.28.2-preview 115 3/6/2024
1.27.16-preview 103 3/2/2024
1.27.12-preview 91 2/23/2024
1.27.11-preview 132 2/19/2024
1.27.5-preview 484 1/9/2024
1.27.2-preview 485 12/28/2023
1.27.1-preview 109 12/28/2023
1.26.61-preview 96 12/17/2023

Changes in bUnit 1.37.7

### Added

- Added support for `RendererInfo` and `AssignedRenderMode` (`.net9.0`).

See the full changelog at https://github.com/bUnit-dev/bUnit/releases