Brian Mains wrote a nice post on how to modify the code for better testability. Now, it’s not that the code is not nicer, but testability hasn’t changed – it was testable before and it remained so after.
I’d like to repeat Roy’s comments on the blog:
we focused a lot of effort in making sure that you need to write very little code to make things testable where possible.
in this case the same result can be achieved with:
//Arrange
Isolate.WhenCalled(() => HttpContext.Current.Handler).WillReturn(page);
var fakeTraceContext = ((Page)HttpContext.Current.Handler).Trace;
//act goes here
Isolate.Verify.WasCalledWithAnyArguments(() => { fakeTraceContext.Warn(null, null, null); });
//this works because all fakes are recursive with Typemock – so the trace, handler props are already fake. just use them as you with to verify calls.
Recursive fakes are very powerful:
- They allow you to write less test code and still achieve the same goal.
- You don’t need to set behavior on what you don’t need
- Tests are more robust, because there’s less to break
And at the end of the day, remember that testability is not the issue. It’s your code – and because you’re going to live with it for a long time, make sure you design it they way you want.