简体   繁体   中英

MS Fakes Static Method that Returns Class with Private Constructor

I'm trying to fake/stub out

System.DirectoryServices.ActiveDirectory.Domain.GetComputerDomain().Name

I'd like to know how to assign GetComputerDomain to return a Domain with a Name of "TestDomain". I can return a null domain as follows:

System.DirectoryServices.ActiveDirectory.Fakes.ShimDomain
    .GetComputerDomain = () => { return null; };

But I think the main issue is that the Domain class does not have a public constructor so I can't do the following:

System.DirectoryServices.ActiveDirectory.Fakes.ShimDomain
    .GetComputerDomain = () => 
    {
        return new System.DirectoryServices.ActiveDirectory.Domain()
        {
            Name = "TestDomain"
        };
    };

How do I get around this issue? I don't think it's possible with Moq alone which I am using along side of MS Fakes. Is it possible to use one, the other, or both to accomplish this? If not what are my other alternatives?

Side note: I'm not really looking for alternatives to getting domain name. I'd really like to how to use this with my current implementation as I want a better understanding of how to mock and fake things out that may fall under this category in the future. Alternatives are welcome but really looking forward to answer to existing question.

The class Domain don't have a c'tor so you will need to fake the future instance of the class that will be created by calling "GetComputerDomain()" and modify the behavior of the "Name" property to return "TestDomain" . It is possible and pretty easy to do so with Typemock Isolator , as shown in the following example:

public class UnitTest
    {
        [TestMethod,Isolated]
        public void GetDomainFakeName_willReturnFakeName()
        {
            var fakeDomain = Isolate.Fake.NextInstance<Domain>();
            Isolate.WhenCalled(() => System.DirectoryServices.ActiveDirectory.Domain.GetComputerDomain()).WillReturn(fakeDomain);
            Isolate.WhenCalled(() => fakeDomain.Name).WillReturn("TestDomain");

            var result = ClassUnderTest.SomeMethod();

            Assert.AreEqual("TestDomain", result);
        }
    }

    public class ClassUnderTest
    {
        public static string SomeMethod()
        {
          return System.DirectoryServices.ActiveDirectory.Domain.GetComputerDomain().Name;
        }
    }

If you just want to use Fakes, this worked for me

    [TestMethod]
    public void TestDomain()
    {
        using (ShimsContext.Create())
        {
            System.DirectoryServices.ActiveDirectory.Fakes.ShimDomain.GetComputerDomain = () =>
            {
                return new System.DirectoryServices.ActiveDirectory.Fakes.ShimDomain();
            };

            System.DirectoryServices.ActiveDirectory.Fakes.ShimActiveDirectoryPartition.AllInstances.NameGet =
                partition =>
                {
                    return "My Name";
                };

            string curName = System.DirectoryServices.ActiveDirectory.Domain.GetComputerDomain().Name;
            Assert.AreEqual("My Name", curName);
        }
    }

Two things to note

  • Return a shimmed object from the static Get...Domain methods
  • To find the Name property, had to use ActiveDirectoryPartition class since Domain is a subclass of ActiveDirectoryPartition and that is where it is defined.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM