Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何测试抽象类中定义的虚拟方法?_C#_Unit Testing_Testing - Fatal编程技术网

C# 如何测试抽象类中定义的虚拟方法?

C# 如何测试抽象类中定义的虚拟方法?,c#,unit-testing,testing,C#,Unit Testing,Testing,我需要对抽象类中定义的虚拟方法进行单元测试。但是基类是抽象的,所以我不能创建它的实例。你建议我做什么 这是以下问题的后续问题:我不确定您的抽象类是什么样子的,但是如果您有类似的内容: public abstract class SomeClass { public abstract bool SomeMethod(); public abstract int SomeOtherMethod(); public virtual int MethodYouWantToTe

我需要对抽象类中定义的虚拟方法进行单元测试。但是基类是抽象的,所以我不能创建它的实例。你建议我做什么


这是以下问题的后续问题:

我不确定您的抽象类是什么样子的,但是如果您有类似的内容:

public abstract class SomeClass
{
    public abstract bool SomeMethod();

    public abstract int SomeOtherMethod();

    public virtual int MethodYouWantToTest()
    {
        // Method body
    }
}
然后,正如@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
}
然后您只需为单元测试实例化
Test

[TestClass]
public class UnitTest1
{
    [TestMethod]
    public void TestMethod1()
    {
        SomeClass test = new Test();
        // Insert whatever value you expect here
        Assert.AreEqual(10, test.MethodYouWantToTest());
    }
}
public void TestMethod()
{
    // arrange
    // act
    // assert
}

没有规则说单元测试不能定义自己的类。这是一种相当普遍的做法(至少对我来说是这样)

考虑标准单元测试的结构:

[TestClass]
public class UnitTest1
{
    [TestMethod]
    public void TestMethod1()
    {
        SomeClass test = new Test();
        // Insert whatever value you expect here
        Assert.AreEqual(10, test.MethodYouWantToTest());
    }
}
public void TestMethod()
{
    // arrange
    // act
    // assert
}
“安排”步骤可以包括任何合理的操作(在测试之外没有副作用),这些操作可以设置您要测试的内容。这可以很容易地包括创建一个类的实例,该类的唯一目的是运行测试。例如,类似这样的内容:

private class TestSubClass : YourAbstractBase { }

public void TestMethod()
{
    // arrange
    var testObj = new TestSubClass();

    // act
    var result = testObj.YourVirtualMethod();

    // assert
    Assert.AreEqual(123, result);
}

测试本身可以有一个从抽象类继承的测试类。然后测试可以实例化该实例并测试其功能。这更像是一个“安排”步骤,但我认为仍然是一个有效的步骤。我同意@David的观点。谢谢。在测试类中,
MethodYouWantToTest
在哪里被调用?@Tim我提供了一个例子-你可以实例化
test
并像使用抽象类一样使用它(这就是为什么您不想覆盖
Test
类中的
MethodYouWantToTest
——这样您就知道您正在从基类运行代码)。