简体   繁体   English

使用 MOQ 在模拟上查找方法设置

[英]Find the methods setup on mock using MOQ

Is there any way to list all the mocked methods (Setups) on a Mock?有没有办法在 Mock 上列出所有模拟方法(设置)?

Lets say, I create mock as following:可以说,我创建模拟如下:

Mock<IAnInterface> aMock = new Mock<IAnInterface>();
aMock.Setup(am => am.Execute()).Returns(true);

Now, i need to find if Execute has been setup on aMock ?现在,我需要找到,如果Execute一直设置aMock

The Mock<> type will have a private Moq.SetupCollection named 'Setups'. Mock<> 类型将有一个名为“Setups”的私有 Moq.SetupCollection。 While Moq.SetupCollection has a private List<Moq.Setup> named 'setups'.虽然 Moq.SetupCollection 有一个名为“setups”的私有 List<Moq.Setup>。

Easy to check while debugging调试时易于检查

Or you can do so with reflection:或者你可以通过反射来做到这一点:

var bindings = BindingFlags.Instance | BindingFlags.NonPublic;
var setupCollectionProperty = Mapper.GetType().GetProperty("Setups", bindings);
var setupCollection = setupCollectionProperty.GetValue(Mapper);
var setupList = setupCollection.GetType().GetField("setups", bindings);
var setups = setupList.GetValue(setupCollection);

I do not know of way of interrogating MOQ (programmatically) about what setups are configured but, running MOQ 4.0, it does list, in the test failed details, which setups were configured.我不知道询问 MOQ(以编程方式)关于配置了哪些设置的方式,但是在运行 MOQ 4.0 时,它确实在测试失败的详细信息中列出了配置了哪些设置。
配置设置的最小起订量报告

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

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