mos*_*o87 4 c# unit-testing moq
我有一个抽象类:
public abstract class MyClass
{
protected abstract bool IsSavable();
protected bool IsExecutable()
{
//New mode or edit mode
if (ViewMode == ViewMode.New || ViewMode == ViewMode.Edit)
{
return IsSavable();
}
//Search mode
if (ViewMode == ViewMode.Search)
{
return true;
}
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
我想对这门课进行单元测试.因此,我需要模拟"IsSavable"方法.它应该总是返回"true".
我知道如何用Moq模拟我的抽象类.但是,如何模拟我的抽象受保护方法,使其返回true?
函数IsSavable在我的抽象类中通过具体方法(IsExecuteable)调用.我想测试一下这个方法.我知道大多数人会建议在实现"IsSavable"的类中测试它们.不幸的是,这将是很多类,我想测试我的方法IsExecutable只有一次.
Jon*_*eet 10
我想对这门课进行单元测试.因此,我需要模拟"IsSavable"方法.它应该总是返回"true".
不,这是一个不成功的人.你可以创建一个你想要的子类:
// Within your test code
class MyClassForTest : MyClass
{
// TODO: Consider making this a property
protected override bool IsSavable()
{
return true;
}
}
Run Code Online (Sandbox Code Playgroud)
然后,当您想要运行测试时,创建一个MyClassForTest而不是仅仅的实例MyClass.
就个人而言,我更喜欢使用模拟框架来实现依赖,而不是我正在测试的类.
| 归档时间: |
|
| 查看次数: |
3372 次 |
| 最近记录: |