问题
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 sinceDomain
is a subclass ofActiveDirectoryPartition
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