C#Microsoft假装测试构造函数中调用的方法
我想在Manager类中测试一个公共函数。问题是该函数是在Manager类的构造函数中调用的。因此,当我创建管理器的新实例时,也会调用该函数 这里我有两个类:一个是Manager类,一个是ManagerTest类来测试管理器 班级经理.csC#Microsoft假装测试构造函数中调用的方法,c#,microsoft-fakes,shim,C#,Microsoft Fakes,Shim,我想在Manager类中测试一个公共函数。问题是该函数是在Manager类的构造函数中调用的。因此,当我创建管理器的新实例时,也会调用该函数 这里我有两个类:一个是Manager类,一个是ManagerTest类来测试管理器 班级经理.cs public class Manager { public Manager(ILogger logger, IMonitor monitor) { //do stuff MethodNeedToBeTested
public class Manager
{
public Manager(ILogger logger, IMonitor monitor)
{
//do stuff
MethodNeedToBeTested();
}
public void MethodNeedToBeTested()
{
}
}
类ManagerTest.cs
[TestClass]
public class ManagerTest
{
[TestMethod]
[DataTestMethod]
public async Task TestMethod()
{
using (ShimsContext.Create())
{
ShimManager.AllInstances.MethodNeedToBeTested = ()=>
{
//shim method here
};
Manager mng= new Manager(_logger, _monitor);
mng.MethodNeedToBeTested(); // the problem is that this line goes directly to the shim above, it doesn't go to the real MethodNeedToBeTested()
}
}
因此,基本上,我只想第一次(在调用构造函数时)填充methodneedtobetest()方法
第二次调用methodneedtobetest方法时,它将转到real methodneedtobetest()函数,而不是Shim可能会帮助您。