Skip to content

BUnit does not work with Blazorise Components #1023

Closed

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

Activity

  1. egil commented on Mar 8, 2023

    @egil
    Member

    That's odd, since I know the folks from Blazorise (@Megabit) use bUnit to test Blazorise with themselves 🙂

    Perhaps @stsrki has a tutorial on how to get started testing components that use Blazorise components.

  2. egil commented on Mar 8, 2023

    @egil
    Member

    Ill convert this to a QA discussion.

  3. locked and limited conversation to collaborators on Mar 8, 2023
  4. converted this issue into a discussion #1024 on Mar 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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