2 places I wish I could use TypeMock – part 1
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.
Recent Posts
- Top 5 questions to ask when checking references
- Unacceptable: Unit testing will take 20 years to catch on
- The 4 reasons why we DIDN’T choose Oslo
- Typemock Academy Launch
- The First Rule to Software Craftsmanship
Categories
- .NET Tests
- Agile
- Code Integrity
- Community
- Debugging
- Fun
- Management for Geeks
- Marketing
- Product
- Release
- Reviews
- SharePoint
- TDD
- Time Management
- Uncategorized
- Unit Tests
Archives
- August 2010
- May 2010
- April 2010
- March 2010
- February 2010
- December 2009
- October 2009
- September 2009
- August 2009
- July 2009
- June 2009
- May 2009
- April 2009
- March 2009
- February 2009
- December 2008
- November 2008
- August 2008
- July 2008
- May 2008
- April 2008
- February 2008
- January 2008
- December 2007
- November 2007
- October 2007
- September 2007
- August 2007
- July 2007
- June 2007
- May 2007
- April 2007
- March 2007
- February 2007
- January 2007
- December 2006
- November 2006
- October 2006
- September 2006
