This package contains an abstraction of the static System.AppDomain class found in .NET Framework 4.8 and above. It allows for an IAppDominFactory interface to be used instead of the static class and for mocks to be created.
$ dotnet add package Agilent.Testables.AppDomainThis package contains an abstraction of the static System.AppDomain class found in .NET Framework 4.8 and above. It allows for an IAppDominFactory interface to be used instead of the static class and for mocks to be created.
IAppDomainFactory as a dependency via the constructor into the class utilize IAppDomain:public class TestableClass
{
private readonly IAppDomainFactory _appDomainFactory;
public TestableClass(IAppDomainFactory appDomainFactory)
{
_appDomainFactory = appDomainFactory;
}
public string GetBaseDirectory()
{
IAppDomain appDomain = _appDomainFactory.CurrentDomain;
return _appDomain.BaseDirectory;
}
}
IAppDomainFactory and Agilent.Testables.AppDomainFactory:_services.AddTransient<IAppDomainFactory, AppDomainFactory>();
[TestMethod]
public void Should_GetBaseDirectory()
{
// Arrange
var baseDirectory = @"C:\AppBaseDirectory";
var mockAppDomain = new Mock<IAppDomain>();
mockAppDomain.Setup(x => x.BaseDirectory).Returns(baseDirectory);
var mockAppDomainFactory = new Mock<IAppDomainFactory>();
mockAppDomainFactory.Setup(x => x.CurrentDomain).Returns(mockAppDomain.Object);
var sut = new TestableClass(mockAppDomainFactory.Object);
// Act
var result = sut.GetBaseDirectory();
// Assert
result.Should().Be(baseDirectory);
}