Wednesday, August 19, 2009

Stepping into methods with yield return

I had a failing unit test (Rhino Expected Call wasn't happening) so I ran with the debugger to try and investigate. When I got to the method in question I tried to step into it but the debugger acted like I had pressed Step Over. The output window had something like this in it: "Step into: Stepping over method without symbols".

After some investigation I figured out the issue. The method in question was returning an IEnumerable and was implemented using "yield return". My test had code like:

var result = callTheMethodInQuestion();
repository.VerifyAll();

Can you see the issue yet?

I was never enumerating result, so the method in question was never actually running, which is why my expected call didn't happen. Slapping a .ToList() onto result triggered the method evaluation and everything worked fine.

3 comments:

Anonymous said...

Your conclusion is not entirely correct. There's a method that's called, even when you use yield and not ToList(), which is part of the framework, which returns the expression, but doesn't evaluate it. This framework method could not be loaded because its symbols weren't there (you wouldn't get the message "stepping over method without symbols" if there wasn't anything to step into in the first place).

This is a known issue with VS2008 SP1 and happens for many other framework classes. It is reported as a bug on Microsoft's feedback forum

Abel Braaksma

Unknown said...

Thanks very much! I was having the same problem, but didn't catch what you did. Makes sense now that I know why. :) Saved me some trouble.

paul smith said...

Thanks very much!