Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

Running my test through nCrunch fails to display value of variable in foreach loop
DotNetKiwi
#1 Posted : Tuesday, September 27, 2016 10:05:39 PM(UTC)
Rank: Newbie

Groups: Registered
Joined: 12/30/2013(UTC)
Posts: 6
Location: New Zealand

Was thanked: 1 time(s) in 1 post(s)
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,
Remco
#2 Posted : Tuesday, September 27, 2016 11:33:41 PM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 7,145

Thanks: 959 times
Was thanked: 1290 time(s) in 1196 post(s)
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.
DotNetKiwi
#3 Posted : Wednesday, September 28, 2016 1:16:18 AM(UTC)
Rank: Newbie

Groups: Registered
Joined: 12/30/2013(UTC)
Posts: 6
Location: New Zealand

Was thanked: 1 time(s) in 1 post(s)
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 :-)
1 user thanked DotNetKiwi for this useful post.
Remco on 9/28/2016(UTC)
Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

YAF | YAF © 2003-2011, Yet Another Forum.NET
This page was generated in 0.026 seconds.
Trial NCrunch
Take NCrunch for a spin
Do your fingers a favour and supercharge your testing workflow
Free Download