Tim*_*Tim 0 c# testing unit-testing
我需要对抽象类中定义的虚拟方法进行单元测试。但基类是抽象的,所以我无法创建它的实例。你建议我做什么?
这是以下问题的后续问题:我正在考虑是否可以通过抽象类的子类的实例进行测试。这是个好办法吗?我该怎么做?
我不确定你的抽象类是什么样子,但如果你有类似的东西:
public abstract class SomeClass
{
public abstract bool SomeMethod();
public abstract int SomeOtherMethod();
public virtual int MethodYouWantToTest()
{
// Method body
}
}
Run Code Online (Sandbox Code Playgroud)
然后,正如 @David 在评论中建议的那样:
public class Test : SomeClass
{
// You don't care about this method - this is just there to make it compile
public override bool SomeMethod()
{
throw new NotImplementedException();
}
// You don't care about this method either
public override int SomeOtherMethod()
{
throw new NotImplementedException();
}
// Do nothing to MethodYouWantToTest
}
Run Code Online (Sandbox Code Playgroud)
然后你只需实例化Test你的单元测试:
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
SomeClass test = new Test();
// Insert whatever value you expect here
Assert.AreEqual(10, test.MethodYouWantToTest());
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3777 次 |
| 最近记录: |