Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find the methods setup on mock using MOQ

Tags:

c#

moq

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?

like image 896
Azodious Avatar asked Mar 20 '13 11:03

Azodious


1 Answers

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);
like image 110
jason Avatar answered Oct 21 '22 00:10

jason