There is no registered service of type 'Microsoft.AspNetCore.Hosting.IWebHostEnvironment #1711
-
| Hi. I have a simple .net 8 blazor server project, and I am using this code @using Microsoft.AspNetCore.Hosting; Works fine When I try to Use Bunit, .... ) get the following error What am I doing wrong... TIA, charlie |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
| Hey @curtic00 The DI-Container of bUnit is disconnected from the one in your FakingSo 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 string WebRootPath { get; set; } public IFileProvider WebRootFileProvider { get; set; } }MockingOr use a mocking library like NSubstitute: public void Test_HomePage_Caption() { var hostEnv = Substitute.For<IWebHostEnvironment>(); hostEnv.EnvironmentName.Returns("Development"); Services.AddScoped<IWebHostEnvironment>(_ => hostEnv); // ... } |
Beta Was this translation helpful? Give feedback.
-
| Hi Steven Faking works a treat. Thanks Charlie From: Steven Giesel ***@***.***> Sent: 25 April 2025 18:35 To: bUnit-dev/bUnit ***@***.***> Cc: curtic00 ***@***.***>; Mention ***@***.***> Subject: Re: [bUnit-dev/bUnit] There is no registered service of type 'Microsoft.AspNetCore.Hosting.IWebHostEnvironment (Discussion #1711) Hey @curtic00 <https://github.com/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 string WebRootPath { get; set; } public IFileProvider WebRootFileProvider { get; set; } } Mocking Or use a mocking library like NSubstitute: public void Test_HomePage_Caption() { var hostEnv = Substitute.For<IWebHostEnvironment>(); hostEnv.EnvironmentName.Returns("Development"); Services.AddScoped<IWebHostEnvironment>(_ => hostEnv); // ... } — Reply to this email directly, view it on GitHub <#1711 (comment)> , or unsubscribe <https://github.com/notifications/unsubscribe-auth/AUSOJFWAFRNTPAR2O5NX6OD23JW4HAVCNFSM6AAAAAB334L6RSVHI2DSMVQWIX3LMV43URDJONRXK43TNFXW4Q3PNVWWK3TUHMYTEOJUHE4TANA> . You are receiving this because you were mentioned. <https://github.com/notifications/beacon/AUSOJFXM37R3TYWHHX2KQGT23JW4HA5CNFSM6AAAAAB334L6RSWGG33NNVSW45C7OR4XAZNRIRUXGY3VONZWS33OINXW23LFNZ2KUY3PNVWWK3TUL5UWJTQAYWMZA.gif> Message ID: ***@***.*** ***@***.***> > |
Beta Was this translation helpful? Give feedback.
Hey @curtic00
The DI-Container of bUnit is disconnected from the one in your
Program.cs, so you have to provide an implementation forIWebHostEnvironment. Either via mocking or providing a fake:Faking
So basically you can create your own implementation: