简体   繁体   English

具有多个约束的单个泛型参数的 Moq 方法

[英]Moq method with single generic parameter with multiple constraints

I have the following method signature that I would like to set up a mock:-我有以下方法签名,我想设置一个模拟:-

public int Update<T>(T item) where T : IItemID, IItemData

When trying to set up the mock (using Moq), I cannot set the correct type for T:-尝试设置模拟(使用 Moq)时,我无法为 T 设置正确的类型:-

mock.Setup(x => x.Update(It.IsAny<???>()).Returns(1);

I've tried with a "fake" interface in my unit test project:-我在我的单元测试项目中尝试过“假”界面:-

public interface IFake : IITemID, IItemData
{
}

mock.Setup(x => x.Update(It.IsAny<IFake>()).Returns(1)

but the verify in my unit test fails because the object I'm using does not inherit from IFake.但是我的单元测试中的验证失败,因为我使用的对象不是从 IFake 继承的。 The object is an actual type that inherits from IItemID and IItemData.该对象是从 IItemID 和 IItemData 继承的实际类型。

Is there anyway to set up the mock for this Update method with multiple constraints?有没有办法为这个具有多个约束的 Update 方法设置模拟?

UPDATE Thanks for the info all.更新感谢所有信息。 After going through this again it looks like the best solution is to not use multiple interface constraints as this can't be mocked.再次经历这个之后,看起来最好的解决方案是不使用多个接口约束,因为这不能被模拟。 I just wondered if there was a solution to this as it seems like something that can cause massive problems later down the line.我只是想知道是否有解决方案,因为这似乎会在以后导致大量问题。

Thanks again!再次感谢!

尝试

mock.Setup(x => x.Update(It.IsAny<It.IsAnyType>()).Returns(1);

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

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