Sunday, June 01, 2008

Mocking templated methods with NMock2

Mocking a method like T GetSomething();

This is something that I have resorted to RinoMock to achieve in the past...
... well it turns out you can do it with NMock2 v1.0 as follows:

Expect.Once.On(a).Method("GetSomething", typeof(int)).Will(Return.Value(42));

I've tried to find this before.. and failed.. so I thought I should blog it to make it easier to find in google.

Thanks to ursenzler for commenting on


Nigel Thorne said...

I may have prematurly posted. I can't find a set of assemblies for NMock2 where this syntax works.

Anyone found one?

Anonymous said...

You can get a version containing the support for generics from

I suggest that you get the latest version directly from the svn repository because the downloadable release is quite old.

Or send me an email to ursenzler(at) and I'll send you the binaries.

Happy mocking

GitHub Projects