Skip to content

BUnit does not work with Blazorise Components #1023

@johniwasz

Description

@johniwasz

Testing any component that that includes a component from Blazorise fails with:

System.InvalidOperationException : Cannot consume scoped service 'Microsoft.AspNetCore.Components.IComponentActivator' from singleton 'Bunit.Rendering.WebTestRenderer'.

OR:

System.InvalidOperationException : Cannot provide a value for property 'JSModule' on type 'Blazorise.Button'. There is no registered service of type 'Blazorise.Modules.IJSButtonModule'.

Example:
Testing this component:

@using Blazorise

<h1>Counter</h1>

<p role="status">Current count: @currentCount</p>

<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>

<Button Clicked="IncrementCount">Click Me (Blazorise)</Button>

With this test:

        [Fact]
        public void CounterStartsAtZero()
        {            
            // Arrange
            var cut = RenderComponent<Counter>();

            // Assert that content of the paragraph shows counter at zero
           cut.Find("p").MarkupMatches("<p role=\"status\">Current count: 0</p>");
        }

Results in this output:

 System.InvalidOperationException : Cannot provide a value for property 'JSModule' on type 'Blazorise.Button'. There is no registered service of type 'Blazorise.Modules.IJSButtonModule'.

Or this test:

        [Fact]
        public void CounterStartsAtZero()
        {
            Services
                .AddBlazorise(options =>
                {
                    options.Immediate = true;
                })
                .AddBootstrapProviders()
                .AddFontAwesomeIcons();
            
            // Arrange
            var cut = RenderComponent<Counter>();

            // Assert that content of the paragraph shows counter at zero
           cut.Find("p").MarkupMatches("<p role=\"status\">Current count: 0</p>");
        }

Results in this output:

 System.InvalidOperationException : Cannot consume scoped service 'Microsoft.AspNetCore.Components.IComponentActivator' from singleton 'Bunit.Rendering.WebTestRenderer'.

Expected behavior:

This line results in successful instantiation of the Counter componet:

        var cut = RenderComponent<Counter>();

Version info:

  • bUnit version: 1.18.4
  • .NET Runtime and Blazor version: .NET 7, Microsoft.AspNetCore.Components.WebAssembly 7.0.3
  • OS type and version: Windows 10

Additional context:

A repo with a reproduction of this error can be found here:

https://github.com/johniwasz/blazorise-bunit-repro

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions