Mephistio
Mephistio

Reputation: 11

Blazor Service does not get injected with bUnit

I have a problem with my .NET 8 Blazor bUnit Tests, where I try to get the services injected with the Services.AddSingleton<>() command, because I have an inject @inject CartService CartService, that causes an error, if I try to run the tests without (see bUnit Website as reference).

But it still is not working and throwing an error, that Services does not have a method called AddSingleton(). Does anybody have an idea, what I can do here?

public void ItemsAreShowedOnOverview() 
{
    Services.AddSingleton<CartService>(new CartService());
    using var ctx = new TestContext();
    var cutOverview = ctx.RenderComponent<Overview>();
    cutOverview.Find("p").MarkupMatches("<p>Taktiles Messgerät</p>");
}

I tried to run without the Services.AddSingleton<>() but it will not work an throw an error, that the service is not registered.

Upvotes: 1

Views: 48

Answers (0)

Related Questions