Moq :: Ignoring Arguments

Created 13 November 2009 08:41
I've always used Rhino Mocks as a mocking framework, but at work we recently made the decision to give Moq a try.

My initial reaction is: WOW! It is just SO much easier to use. I've always found the setting up of mocks and verification of method calls a tedious task, but using Moq I've actually found it's really simple. It's a far more intuitive framework, and once I've got around my Rhino Mock habits, I've really found that the amount of code required for mocking is drastically reduced.

One very simple thing that stumped me today though, was trying to verify a method was called, but telling Moq to ignore the argument that was passed. In this case Rhino Mocks is a little more intuitive - it has an IgnoreArguments() method that chains off the setup. In the end though, the Moq implementation is actually easier - you just make your Setup or Verify call and use the "It" class to generate your stub.

In my example:
  myMockedClass.Verify(x => x.Connect(It.IsAny<MyArgumentType>()));
Alex on 20 September 2010 at 22:37
Ryan on 24 October 2011 at 19:46
Just saw this post today, Thanks! Never would have found the &quot;It&quot; class without this post and Google.
Carl on 13 December 2011 at 16:18
Thanks! Saved me too!
Rupam Srivastava on 26 January 2012 at 13:18
Never knew about this! Made life so easy. Thanks very much.!!