6

Is there any way to list all the mocked methods (Setups) on a 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?

4

2 回答 2

3

Mock<> 类型将有一个名为“Setups”的私有 Moq.SetupCollection。而 Moq.SetupCollection 有一个名为“setups”的私有 List<Moq.Setup>。

调试时易于检查

或者你可以通过反射来做到这一点:

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);
于 2020-12-15T17:52:09.690 回答
1

我不知道询问 MOQ(以编程方式)关于配置了哪些设置的方式,但是,运行 MOQ 4.0,它确实在测试失败的详细信息中列出了哪些设置已配置。
最小起订量报告配置设置

于 2013-03-20T12:21:45.790 回答