Skip to content

Commit 4ddcc59

Browse files
refactor: temporarily (possibly use AutoMoq after I research it) replace TestWebHostEnvironment with Moq for IWebHostEnvironment in WebApplicationFactory
1 parent d1e638e commit 4ddcc59

File tree

1 file changed

+5
-21
lines changed

1 file changed

+5
-21
lines changed

EssentialCSharp.Web.Tests/WebApplicationFactory.cs

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using Microsoft.Extensions.DependencyInjection.Extensions;
99
using Microsoft.Extensions.FileProviders;
1010
using Microsoft.Extensions.Logging;
11+
using Moq;
1112

1213
namespace EssentialCSharp.Web.Tests;
1314

@@ -51,9 +52,11 @@ protected override void ConfigureWebHost(IWebHostBuilder builder)
5152
var fileProvider = new PhysicalFileProvider(testDataPath);
5253
services.AddSingleton<IListingSourceCodeService>(sp =>
5354
{
54-
var mockEnv = new TestWebHostEnvironment(testDataPath, fileProvider);
55+
var mockEnv = new Mock<IWebHostEnvironment>();
56+
mockEnv.Setup(m => m.ContentRootPath).Returns(testDataPath);
57+
mockEnv.Setup(m => m.ContentRootFileProvider).Returns(fileProvider);
5558
var logger = sp.GetRequiredService<ILogger<ListingSourceCodeService>>();
56-
return new ListingSourceCodeService(mockEnv, logger);
59+
return new ListingSourceCodeService(mockEnv.Object, logger);
5760
});
5861
});
5962
}
@@ -92,22 +95,3 @@ protected override void Dispose(bool disposing)
9295
}
9396
}
9497
}
95-
96-
/// <summary>
97-
/// Minimal IWebHostEnvironment implementation that redirects ContentRoot to the TestData directory.
98-
/// </summary>
99-
internal sealed class TestWebHostEnvironment : IWebHostEnvironment
100-
{
101-
public TestWebHostEnvironment(string contentRootPath, IFileProvider contentRootFileProvider)
102-
{
103-
ContentRootPath = contentRootPath;
104-
ContentRootFileProvider = contentRootFileProvider;
105-
}
106-
107-
public string ContentRootPath { get; set; }
108-
public IFileProvider ContentRootFileProvider { get; set; }
109-
public string WebRootPath { get; set; } = string.Empty;
110-
public IFileProvider WebRootFileProvider { get; set; } = new NullFileProvider();
111-
public string EnvironmentName { get; set; } = "Testing";
112-
public string ApplicationName { get; set; } = "EssentialCSharp.Web";
113-
}

0 commit comments

Comments
 (0)