There is a saying in an ancient language: “the shoemaker walks barefoot”. This is still true in our future age, and the TypeMock creators cannot test TypeMock.NET with TypeMock.
Here are 3 places that we had to use other methods:
1. Displaying a Message
Here is the code (it is static as it doesn’t use any member fields):
internal static void ShowMessageBox(AboutMessage msg) { Application.EnableVisualStyles(); Application.Run(new AboutMessageBox(msg,0)); } |
We cannot run this in our tests as a MessageBox will appear and halt our tests.
If we had TypeMock.NET we could test as follows, expecting ShowMessage to be called once.
Mock m = MockManager.Mock(typeof(TestedClass)); m.ExpectCall(“ShowMessageBox”); |
Simple. Here is what we had to do:
#if DEBUG public static bool shouldMock = false; public static int countCalls = 0; #endif internal static void ShowMessageBox(AboutMessage msg) { #if DEBUG if (shouldMock) { countCalls++; return; } #endif Application.EnableVisualStyles(); Application.Run(new AboutMessageBox(msg,0)); } |
We had to enable the method to not show the MessageBox in certain cases. We decided to keep it all in debug mode only so that the production code is not spoiled.
TestedClass.shouldMock = true; TestedClass.countCalls = 0; … Assert.AreEqual(1,TestedClass.countCalls); TestedClass.shouldMock = false; |
I am sure that there are better ways to do this, but having TypeMock would have really helped keep our code clean.
I just wanted to cocutrsnt a quick remark to be able to say thanks to you for these magnificent tips and hints you are showing at this site. My prolonged internet look up has at the end of the day been rewarded with reliable strategies to talk about with my family and friends. I would suppose that most of us website visitors are truly fortunate to live in a really good community with many marvellous individuals with useful pointers. I feel somewhat happy to have encountered your web page and look forward to tons of more exciting times reading here. Thanks a lot once again for a lot of things.
Just do me a favor and keep writing such trenchant analyses, OK?
Hello I’ll be thus excited I dircevesod the webpage, I seriously located you unintentionally, although I had been exploring upon Google with regards to yet another thing, Anyhow I will be here today and would probably want to point out cheers to acquire a wonderful post plus a all-round thrilling weblog (I in addition seriously like the actual theme/design), I do not have enough time in an effort to browse all of it inside the second but I possess saved that as well as added your own personal Bottles, when I’ve time I am returning to examine much more, Please do sustain the actually astounding job.
That’s a brilliant answer to an interesting question
I’m not easily impressed. . . but that’s impressing me! 🙂