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:
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?
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.