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
?
The Mock<> type will have a private Moq.SetupCollection named 'Setups'. While Moq.SetupCollection has a private List<Moq.Setup> named 'setups'.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With