Skip to content

Agilent/Agilent.Testables.AppDomain

Repository files navigation

Agilent.Testables.AppDomain

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.

Usage

  1. Pass 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;
    }
}
  1. In your IOC Container register IAppDomainFactory and Agilent.Testables.AppDomainFactory:
_services.AddTransient<IAppDomainFactory, AppDomainFactory>();
  1. In your tests, setup and use IAppDomainFactory and IAppDomain as required:
    [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);
    }

About

A testable abstraction of System.AppDomain

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages