MS Fakes Static Method that Returns Class with Private Constructor

岁酱吖の 提交于 2019-12-25 04:22:52

问题


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.


回答1:


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.



回答2:


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;
        }
    }


来源:https://stackoverflow.com/questions/38799508/ms-fakes-static-method-that-returns-class-with-private-constructor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!