简体   繁体   English

如何模拟抽象基类

[英]How to mock An Abstract Base Class

I have a base class called "Question" and several child classes such as "TrueFalse", "MultipleChoice", "MatchPairs" etc...我有一个名为“Question”的基类和几个子类,例如“TrueFalse”、“MultipleChoice”、“MatchPairs”等......

The base class has methods with logic that all of the child classes use, such as sending off scores and raising events.基类具有所有子类都使用的具有逻辑的方法,例如发送分数和引发事件。

I have set my unit tests up for the child classes but I am not sure how I can setup unit tests for the methods in the base class.我已经为子类设置了单元测试,但我不确定如何为基类中的方法设置单元测试。

I did some searching and I understand I need to create a Mock of the class but I am not sure how to do this as I have only seen how to do this on an instantiable object.我做了一些搜索,我知道我需要创建一个类的 Mock,但我不知道如何做到这一点,因为我只看到了如何在可实例化的对象上做到这一点。

I have Moq & NUnit installed in project so ideally id like to use this.我在项目中安装了 Moq 和 NUnit,所以理想情况下我喜欢使用它。 I am still new to programming and this is my first time adding unit tests so I appreciate any advice you can give me.我还是编程新手,这是我第一次添加单元测试,所以我很感激你能给我的任何建议。

I did a search on site first and found a couple of similar questions but they did not give any example on how to do it, just that it needed to be mocked.我首先在网站上进行了搜索,发现了几个类似的问题,但他们没有给出任何关于如何做的示例,只是需要对其进行模拟。

Many thanks.非常感谢。

From this answer it looks like what you need is something along these lines:这个答案看来,您需要的是以下方面的内容:

[Test]
public void MoqTest()
{
    var mock = new Moq.Mock<AbstractBaseClass>();            
    // set the behavior of mocked methods
    mock.Setup(abs => abs.Foo()).Returns(5);

    // getting an instance of the class
    var abstractBaseClass = mock.Object;
    // Asseting it actually works :)
    Assert.AreEqual(5, abstractBaseClass.Foo());
}

I was trying to mock an abstract class and this way didn't worked for me.我试图模拟一个抽象类,但这种方式对我不起作用。 what did work was to create a new class that extended the abstract class所做的工作是创建一个扩展抽象类的新类

class newclass : abstractClass
{
}

like this I could set the property and test the main method像这样我可以设置属性并测试主要方法

There is no simple way to test it,没有简单的方法可以测试它,

The best option is:最好的选择是:

  • mark base class methods as virtual将基类方法标记为虚拟
  • create test classes for each of the "TrueFalse", "MultipleChoice", "MatchPairs" classes with virtual methods overridden and invoke public Abstract method.为每个“TrueFalse”、“MultipleChoice”、“MatchPairs”类创建测试类,虚拟方法被覆盖并调用公共抽象方法。

So for example you have next inheritance structure所以例如你有下一个继承结构

class Question {
     protected virtual bool CheckCorrect(params int[] answers){
          return answers.Any(x => x== 42);
     }
}

class TrueFalse: Question {
     
     public int SelectedAnswer {get;set;}
     public bool IsCorrect(){
          return CheckCorrect(SelectedAnswer );
     }
}
class MultipleChoice: Question {
     public int[] SelectedAnswers {get;set;}
     public bool IsCorrect(){
          return CheckCorrect(SelectedAnswers );
     }
}

Test methods for this:测试方法:

abstract class TrueFalseTest: TrueFalseTest{

     public abstract bool CheckCorrectReal(params int[] answers);
     
     public override bool CheckCorrect(params int[] answers){
          return CheckCorrect(SelectedAnswer );
     }
}

abstract  class MultipleChoiceTest: MultipleChoice {
     public abstract bool CheckCorrectReal(params int[] answers);
     
     public override bool CheckCorrect(params int[] answers){
          return CheckCorrect(SelectedAnswer );
     }
}

And test methods itself:和测试方法本身:

class TestQuestionForms{
    [Fact]
    public void TrueFalseTest_ShouldExecute_CheckCorrectReal()
    {
        //setup
        var mock = new Mock<TrueFalseTest>();

        mock.Setup(q => q.CheckCorrectReal(It.IsAny<int[] answers>))
            .Returns(true);

        //action
        mock.Object.IsCorrect();
       
       //assert
       mock.Verify(db => db.CheckCorrectReal());

    }
    [Fact]
    public void MultipleChoiceTest_ShouldExecute_CheckCorrectReal()
    {   
        //setup
        var mock = new Mock<MultipleChoiceTest>();

        mock.Setup(q => q.CheckCorrectReal(It.IsAny<int[] answers>))
            .Returns(true);

        //action
        mock.Object.IsCorrect();
       
       //assert
       mock.Verify(db => db.CheckCorrectReal());

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM