Typemock AAA - Faking the same method with different parameters

25 February 2009 · 2 minute read · Unit Testing · Umbraco · Typemock

As I stated in my last post (oh so 5 minutes ago! :P) I'm working on a new project for the Umbraco team, one thing I'm really focusing hard on with LINQ to Umbraco is Test Driven Development (TDD), and with that I'm using Typemock as my mocking framework (since I scored a free license I thought I should use it).

The Arrange, Act, Assert (AAA) is really sweet, but it does have a problem, it doesn't support mocking a method call with different parameters. I can't call the same method 3 times and have a different output depending on what was passed in.

Makes for a bit of a problem when you want to test conditionals against your mock. I have requested the feature, but for the time being I found a nice little work-around, Extension Methods!

So I'm mocking the IRecordsReader from the Umbraco DataLayer, and I want to have something different returned depending on the parameter of the GetString method, so I created extensions like this:

public static string GetName(this IRecordsReader reader){
return reader.GetString("Name");
}

Now I can easily do this:

Isolate.WhenCalled(() => fakeReader.GetName()).WillReturn("Name");
Isolate.WhenCalled(() => fakeReader.GetString("SomethingElse")).WillReturn("Not Name");

// do something with fakeReader

Isolate.Verify.WasCalledWithExactArguments(() => fakeReader.GetName());
Isolate.Verify.WasCalledWithExactArguments(() => fakeReader.GetString("SomethingElse"));

This obviously isn't the best way to do it, does mean that you have to then use extension methods when you are writing the code to use it.
But that's not really a problem for me at the moment, I'm doing a lot of the same data reading from the IRecordsReader so I can easily do the extension method.

Now if they will just add the support like Rhino Mocks has then it'll be sweet!


Published: 2017-10-23 09:53:42 +1100 +1100, Version: 4113ab4