简体   繁体   English

您可以模拟实现接口和抽象类的对象吗?

[英]Can you mock an object that implements an interface AND an abstract class?

Is it possible to use Moq to mock an object that implements an interface and abstract class? 是否可以使用Moq模拟实现接口和抽象类的对象?

Ie: 即:

public class MyClass: SomeAbstractClass, IMyClass

Can you mock this? 你可以嘲笑吗?

You can mock any interface, and any abstract or virtual members. 您可以模拟任何接口,以及任何抽象或虚拟成员。 That's basically it. 基本上就是这样。

This means that the following are absolutely possible: 这意味着绝对有可能做到以下几点:

var imock = new Mock<IMyClass>();
var aMock = new Mock<SomeAbstractClass>();

If the members inherited from SomeAbstractClass aren't sealed, you can also mock MyClass: 如果没有密封从SomeAbstractClass继承的成员,则还可以模拟MyClass:

var mcMock = new Mock<MyClass>();

Whether this makes sense or not depends on the implementation of MyClass. 这是否有意义取决于MyClass的实现。 Let's say that SomeAbstractClass is defined like this: 假设SomeAbstractClass的定义如下:

public abstract class SomeAbstractClass
{
    public abstract string GetStuff();
}

If the GetStuff method in MyClass is implemented like this, you can still override it: 如果MyClass中的GetStuff方法是这样实现的,您仍然可以覆盖它:

public override string GetStuff()
{
    return "Foo";
}

This would allow you to write: 这将使您可以编写:

mcMock.Setup(x => x.GetStuff()).Returns("Bar");

since unless explicitly sealed, GetStuff is still virtual. 因为除非明确密封,否则GetStuff仍然是虚拟的。 However, had you written GetStuff like this: 但是,您是否像这样编写了GetStuff:

public override sealed string GetStuff()
{
    return "Baz";
}

You wouldn't be able to mock it. 您将无法模拟它。 In that case, you would get an exception from Moq stating that it's an invalid override of a non-virtual member (since it's now sealed ). 在这种情况下,您会从Moq处获得一个异常,指出它是对非虚拟成员的无效替代(因为它已经被sealed )。

Your question does not really make sense. 您的问题没有任何意义。 Moq can be used to mock abstract classes and interfaces. Moq可用于模拟抽象类和接口。 Since MyClass is neither then you cannot create a mock of it. 由于MyClass都不是,因此您无法创建它的模拟。 I don't think this is a problem though, since consumers of your MyClass instance will probably be expecting a SomeAbstractClass or an IMyClass instance and not a MyClass instance. 我认为这不是问题,因为MyClass实例的使用者可能期望使用SomeAbstractClassIMyClass实例,而不是MyClass实例。 If indeed they expect a MyClass instance, then you need to make some abstraction on top of it. 如果确实希望使用MyClass实例,则需要在其之上进行一些抽象。 This can be achieved by either having SomeAbstractClass implement the IMyClass interface, or by having the IMyClass interface expose the methods and properties of SomeAbstractClass . 这可以通过使SomeAbstractClass实现IMyClass接口或通过使IMyClass接口公开IMyClass的方法和属性来SomeAbstractClass

暂无
暂无

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

相关问题 从抽象类继承并实现接口的模拟类 - Mock class which inherit from abstract class and implements interface 如何模拟扩展类并实现接口的对象? - How to mock an object that extends a class and implements an interface? 犀牛模拟接口IA,该接口使用抽象类aB实现接口IB - rhino mock an interface IA which implements interface IB with abstract class aB 类都扩展了抽象类并实现了接口 - Class both extends an abstract class and implements an interface 强制转换实现接口的通用抽象类 - Casting in generic abstract class that implements interface 您可以通过实现该接口的类型的强制类型转换和null检查来对接口的Moq模拟进行传递吗? - Can you make a Moq mock of an interface pass a cast and null check of a type that implements that interface? 使用抽象类和接口的原因? (抽象class实现接口) - Reason to use BOTH abstract classes and interfaces? (Abstract class implements interface) 对实现通用接口的抽象类执行“保护的抽象替代” - Perform a 'protected abstract override' for an abstract class which implements a generic interface 我怎样才能拥有一个实现接口并继承自抽象 class 的 class? (C# .NET 3.5) - How can I have a class that implements an interface and inherits from an abstract class? (C# .NET 3.5) 如何创建一个 List(of T),其中 T 是 (T) 的接口,List 的内容继承了一个实现 T 接口的抽象类 - How can I create a List(of T) where T is an interface of (T) and the contents of the List inherit an Abstract class that implements the interface of T
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM