简体   繁体   English

如何使用 MsTest Moq 在继承 Abstract class 的 class 中模拟方法

[英]How to Mock a method in a class that inherited Abstract class using MsTest Moq

internal abstract class ClassA {
{
  internal abstract Task getInfoById(int id);
  internal async Task getDetails()
  {
    // some details
  }
}
public class ClassB: ClassA {
 internal override async Task getInfoById(int id){
    //some code processing
   await getDetails();
   }
 }

In the above code, How to mock getDetails() while writing test for getInfoById method for classB using MsTest and Moq在上面的代码中,How to mock getDetails() while writing test for getInfoById for classB using MsTest and Moq

I'm trying to imagine where "doSomething" is in your ClassB class as it isn't there at the time of this answer.我试图想象“doSomething”在您的 ClassB class 中的位置,因为在此答案时它不存在。 That said, you should not mock "getDetails" as it is technically part of the service under test (by virtue of it being a component of the base class).也就是说,您不应该模拟“getDetails”,因为它在技术上是被测服务的一部分(因为它是基类的一个组件)。 It would be appropriate to extract the functionality of "getDetails" out to a separate class with an interface of which you could mock, however.但是,将“getDetails”的功能提取到一个单独的 class 中是合适的,但是您可以模拟它的接口。

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

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