Skip to main content

Verifying Mock Parameters

Found a use case for a feature of the .NET mocking framework Moq that may not be that well known, and was new to me at least.

I had a method under test that - via a number of overloaded extension methods - called an underlying method in the Umbraco CMS. The details aren't really important, but specifically it was the method GetImageUrl on IImageUrlGenerator that I was mocking to return a known output. I was then testing how my method used that result it in it's processing.

I could see the method failing when I had a setup like this:

But passed with this:

So clearly we weren't passing in the Height parameter that I was expecting.

One way to solve this would be to get the Umbraco code locally and step into it, but I found another way that I read about on this closed issue for the Moq library.

It provides a means of capturing the parameters passed to a mocked method, and then establishing what they are via a breakpoint or logging.

That revealed that somewhere we'd muddled up the width and height paramters.

At which point it was fairly easy to fix the issue.