Skip to content

There is no registered service of type 'Microsoft.AspNetCore.Hosting.IWebHostEnvironment #1711

Answered by linkdotnet
curtic00 asked this question in Q&A
Discussion options

You must be logged in to vote

Hey @curtic00

The DI-Container of bUnit is disconnected from the one in your Program.cs, so you have to provide an implementation for IWebHostEnvironment. Either via mocking or providing a fake:

Faking

So basically you can create your own implementation:

public void Test_HomePage_Caption()
{
    Services.AddScoped<IWebHostEnvironment>(_ => new FakeWebHostEnvironment());
    
    // ...
}

private sealed class FakeWebHostEnvironment : IWebHostEnvironment
{
    public string EnvironmentName { get; set; } = "Development";
    public string ApplicationName { get; set; }
    public string ContentRootPath { get; set; }
    public IFileProvider ContentRootFileProvider { get; set; }
    public st…

Replies: 2 comments

Comment options

linkdotnet
Apr 25, 2025
Maintainer Sponsor

You must be logged in to vote
0 replies
Answer selected by linkdotnet
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants