Build/Test Issues

Running my test through nCrunch fails to display value of variable in foreach loop

Started by DotNetKiwi on 4,302 views

Hi there,

I've got an issue when using NCrunch 'Debug starting test in new task runner process'.

The snippet of code below is evaluated and the foreach loop is entered, however the value of rateItem shows as being null. This is incorrect of course because if it was null it would not enter the loop. Interestingly though the code does execute as expected, despite rateItem shown as being null.

foreach (var rateItem in importData.RateSetEmployeeRates.Where(r => newRateSets.Contains(r.Rate.RateSetId.GetValueOrDefault(0)))) {
var rate = rateItem.Rate;
...

I checked the forum but couldn't find any mention of this.

If I run the test using MS Test debug, rateItem does show the correct value, so I have a work-around.

I an using NCrunch 2.15.0.9 and Visual Studio 2015 Professional.

Thanks,

Edited

Hi, thanks for sharing this issue.

This problem is being caused by a debug scope issue when NCrunch instruments the assembly on disk. Basically, a method can contain multiple scopes (usually identified by the braces '{ }'). Each scope contains variables. If more than one scope within a method contains the same variable name, the debugger gets confused between them and can often query the wrong one in the IDE. The code is functionally identical, but the loss of scope data in the instrumented assembly (vs the original one) means that during an NCrunch debug session this can unfortunately happen.

It's recognised that this is a problem caused by NCrunch's instrumentation. It can be fixed. However, the solutions involved are not without trade-off and are likely to impact the performance of the instrumentation routine, increasing the time you need to wait for your test results.

Until a proper solution to this problem can be found that doesn't seriously impact performance, I recommend avoiding using the same variable names between scopes within the same method if you wish to evaluate them via tooltip at debug time.
First of all, thanks for the very prompt reply.

In actual fact there were two very similar foreach loops inside of the same method. Each loop uses the same variable names, but only one loop would ever be executed.

I changed the variable names so that each foreach loop now have unique names and this has fixed the issue.

Many thanks :-)

Post a reply

Log in to reply.