【发布时间】:2017-05-05 13:42:39
【问题描述】:
我正在尝试使用 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; }
};
注意:我的抽象基类受保护方法执行复杂的操作,所以我需要模拟它。上面的代码只是一个示例。
任何指针将不胜感激!
【问题讨论】:
-
如果你的目标是 GetSquare() 为什么你不能做类似 var x = _myChildClass.GetSquare(2);然后就可以 Assert.IsEqual(2, x);
-
如前所述请注意:我的抽象基类受保护方法执行复杂的操作,因此我需要模拟它。上面的代码只是一个示例。
标签: c# unit-testing protected microsoft-fakes abstract-base-class