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 :twisted:
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.

  1. Don’t break while recording - There is really no reason to break there as no calls are actually made.
  2. If possible mock Properties with RepeatAlways() to make sure that debugging won’t confuse.
  3. 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
  4. Turn off evaluating Properties:

Bookmark at:
Add 'How to Debug with TypeMock.NET without losing your head' to Del.icio.us Add 'How to Debug with TypeMock.NET without losing your head' to digg Add 'How to Debug with TypeMock.NET without losing your head' to reddit Add 'How to Debug with TypeMock.NET without losing your head' to Feed Me Links! Add 'How to Debug with TypeMock.NET without losing your head' to Technorati Add 'How to Debug with TypeMock.NET without losing your head' to Yahoo My Web Add 'How to Debug with TypeMock.NET without losing your head' to Newsvine Add 'How to Debug with TypeMock.NET without losing your head' to FURL Add 'How to Debug with TypeMock.NET without losing your head' to blinklist Add 'How to Debug with TypeMock.NET without losing your head' to My-Tuts 

8 October 2006 | .NET Tests, Debugging | Comments | Print This Post

Leave a Reply

  1.  
  2.  
  3.  
Search Eli Lopian’s Blog (TypeMock)

Navigation

Recent Posts

Categories

Archives

Managment