模拟实现多个接口的泛型 [英] Mocking generics that implement multiple interfaces

查看:231
本文介绍了模拟实现多个接口的泛型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的类实现,泛型实现了两个接口...

Here's my class implementation where the generic is implementing two interfaces...

public class ClassA<TGeneric> : where TGeneric: IInterfaceA, IInterfaceB

我想模拟ClassA.但是,我不能使用

I want to Mock ClassA. However, I can't use

var mock = new Mock<Class<A<IInterfaceA>>();

var mock = new Mock<Class<A<IInterfaceB>>();

因为通用要求同时实现两个接口.我知道您可以使用最小订量中的As()方法来模拟具有多个接口的对象,但是我这里实际上没有对象,而是通用类型.

since the generic requires implementations of both interfaces. I know you can mock objects with multiple interfaces by using the As() method on the moq, but I don't really have an object here but a generic type.

有想法吗?

谢谢...

推荐答案

您可以定义一个同时包含接口A和B的接口(在测试项目中,出于测试目的),然后在模拟中使用它.

You could define an interface that includes both interface A and B (in your test project, for testing purposes), then use that in your mock.

public interface ICanTestAAndB : IInterfaceA, IInterfaceB {}

var mock = new Mock<ClassA<ICanTestAAndB>>();

这篇关于模拟实现多个接口的泛型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆