How to Debug with TypeMock.NET without losing your head
TypeMock.NET has many internal features that allow debugging code that has some mocked methods, and although this works most of the time, there are some things you need to watch out for. All these have to do with the Visual Studio’s Property Evaluation or funceval (see Mike Stall’s – Func-eval is evil post)
Basically whenever you see in a Property value in a watch or local variable window, the debugger hijacks a thread and evaluates the property.
Now, this works fine if your properties don’t change the state of your code, once they do, this will mess up your code.
The following code:
private int count; public int Count { get { return count++; } }
can really confuse, as while stepping with the debugger count will be increased. The same is true for TypeMock.NET expectations, as long as we are not using RepeatAlways() we are changing the state of our code.
So if we have the following expectation.
using (RecordExpectations recorder = RecorderManager.StartRecording()) { recorder.ExpectAndReturn(SomeProperty, 10); }
and we break just before we call SomeProperty, and evaluate it, we will see that SomeProperty returns 10, but when we continue it won’t return that value any more and the test will fail. Very annoying.
What is worse is trying to step through the recording session. If you break while inside a recording (in the using block), TypeMock will catch all the evaluations and mock them. Using the Tracer while doing this stunt will show how the expectations are being filled…
See how NUnit.Core is being mocked
and the get_MyProperty is also mocked because of funceval calls.
Workaround
Until the debugger comes with a better API here are things to remember.
- Don’t break while recording – There is really no reason to break there as no calls are actually made.
- If possible mock Properties with RepeatAlways() to make sure that debugging won’t confuse.
- This is not always possible as you might sometimes want to validate that a property was called only once, in this case just keep in mind that the debugger can change your programs’ flow
- Turn off evaluating Properties:
- Visual Studio 2003, go to Tools -> Options -> Debugging -> General and uncheck Allow property evaluation in variables windows
- Visual Studio 2005, go to Tools -> Options -> Debugging -> General and uncheck Enable property evaluation and other implicit function calls
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
