Moq Minute - Linq to Moq

Reading time ~1 minute

I love Moq. I think it is the best .NET mocking library. This week I learnt a new feature for simplifying your mock setup: Linq to Mocks.

This feature lets you use Linq like syntax to setup mocks. This is great for really simple mocks. I first started using them as an alternative way to create mocks where I did not care how they were used. Previously I declared a complete mock, but with the new syntax can be much simpler:

using Moq;
using System;

IProgress<int> before = new Mock<IProgress<int>>().Object;

IProgress<int> after = Mock.Of<IProgress<int>>();

Later, a coworker showed me you could setup basic behaviours easily! Using a lambda expression you can specify the method to be called, any constraints on parameters and the return value. In the sample below the method ExecuteScalar can be called to return "return value". Neat eh?

using Moq;
using System.Data;

IDbCommand command = Mock.Of<IDbCommand>(
	x => x.ExecuteScalar() == "return value"
);

The syntax can take some getting used to and looks wonky at first, but for really simple mocks helps simplify your test setup. For more examples read the Moq quickstart.

I hope you like this Moq feature and try it out soon. Enjoy.


I wanted to share this with my co-workers and decided sharing it here would be an even better way to save keystrokes.

The Worst Week of My Life

In January I had the worst week of my life. My wife and I joked we wanted to start 2016 in February. Within a single week I lost my job a...… Continue reading