我正在尝试使用Microsoft.Fakes为下面显示的代码编写单元测试。
作为 Microsoft.Fakes 的新手,我在模拟对抽象基类保护功能方法的调用时遇到了困难
我的基类:
public abstract class MyAbstractBaseClass
{
protected virtual int MyFunctionalBaseClassMethod(int parameter1)
{
return (parameter1 * parameter1);
}
}
我的孩子班:
public class MyChildClass : MyAbstractBaseClass
{
public int GetSquare(int parameter1) //(target method for unit test)
{
return MyFunctionalBaseClassMethod(parameter1); //(target method to mock using Fakes)
}
}
我尝试使用以下单元测试代码进行模拟,但没有奏效:
var square = 10;
var stubMyAbstractBaseClass = new Fakes.StubMyAbstractBase()
{
MyFunctionalBaseClassMethod = (a) => { return square; }
};
注意:我的抽象基类受保护方法执行复杂的操作,所以我需要模拟它。上面的代码只是一个示例。
任何指针将不胜感激!