KeyNotFoundException,但调试时没有

问题描述:

我一直在构建一个扩展库,并且我在http://www.extensionmethod.net上使用了一个很好的扩展方法。在我的单元测试中(使用NUnit 1.5.2),我遇到了一个有趣的问题。首先,让我们来看看代码:KeyNotFoundException,但调试时没有

/// <summary> 
    /// Groups and aggregates the sequence of elements. 
    /// </summary> 
    /// <typeparam name="TSource">The source type in the sequence.</typeparam> 
    /// <typeparam name="TFirstKey">The first key type to group by.</typeparam> 
    /// <typeparam name="TSecondKey">The second key type to rotate by.</typeparam> 
    /// <typeparam name="TValue">The type of value that will be aggregated.</typeparam> 
    /// <param name="source">The source sequence.</param> 
    /// <param name="firstKeySelector">The first key selector.</param> 
    /// <param name="secondKeySelector">The second key selector.</param> 
    /// <param name="aggregator">The aggregating function.</param> 
    /// <returns>A <see cref="Dictionary{TKey,TValue}" /> representing the pivoted data.</returns>  
    public static Dictionary<TFirstKey, Dictionary<TSecondKey, TValue>> Pivot<TSource, TFirstKey, TSecondKey, TValue> 
     (this IEnumerable<TSource> source, 
     Func<TSource, TFirstKey> firstKeySelector, 
     Func<TSource, TSecondKey> secondKeySelector, 
     Func<IEnumerable<TSource>, TValue> aggregator) 
    { 
     return source.GroupBy(firstKeySelector).Select(
      x => new 
      { 
       X = x.Key, 
       Y = x.GroupBy(secondKeySelector).Select(
        z => new { Z = z.Key, V = aggregator(z) }).ToDictionary(e => e.Z, o => o.V) 
      }).ToDictionary(e => e.X, o => o.Y); 
    } 

功能做什么,是发生在类型TSource的IEnumerable和枢轴转动物品放入一个字典,并使用您定义的任何功能汇总的项目。我的样本数据集是一组人(在一个名为Person的类型中)。

 private static readonly Person[] people = 
     new[] 
     { 
      new Person { Forename = "Matt", Surname = "Someone", Email = "[email protected]", Age = 25, IsMale = true }, 
      new Person { Forename = "Chris", Surname = "Someone", Email = "[email protected]", Age = 28, IsMale = false }, 
      new Person { Forename = "Andy", Surname = "Someone", Email = "[email protected]", Age = 30, IsMale = true }, 
      new Person { Forename = "Joel", Surname = "Someone", Email = "[email protected]", Age = 30, IsMale = true }, 
      new Person { Forename = "Paul", Surname = "Someone", Email = "[email protected]", Age = 30, IsMale = true } 
     }; 

最后,我们做我们的测试:

/// <summary> 
    /// Performs a pivot function on the sample array. 
    /// </summary> 
    [Test] 
    public void Pivot() 
    { 
     /* Our sample data is an array of Person instances. 
     * Let's organise it first by gender (IsMale), and then by Age. 
     * Finally, we'll return a count. */ 
     var organised = people.Pivot(p => p.IsMale, p => p.Age, l => l.Count()); 

     Assert.IsTrue(organised.Count == 2, "More than two genders were returned."); 
     Assert.IsTrue(organised[true].Count == 2, "More than two ages were returned for males."); 
     Assert.IsTrue(organised[false].Count == 1, "More than 1 age was returned for females."); 

     int count = organised[true][30];    
     Assert.IsTrue(count == 3, "There are more than 3 male 30 year olds in our data."); 
    } 

什么在这个测试用例返回,是一个字典>实例。布尔值是IsMale组的结果,在我们的示例数据中,正确地返回2个项目,true和false。内部词典有一个关键的年龄和计数值。在我们的测试数据中,有组织的[true] [30]反映了所有30岁以上的男性。

问题不在于函数本身,而是因为某些原因,当我们通过NUnit测试运行器和Resharper的单元测试运行器运行时,测试失败,报告一行KeyNotFoundException“int count = organized [TRUE] [30];”。当我们调试这个测试时,它正确地返回值3(如在我们的样本数据中,我们有3个30岁的男性)。

有什么想法?

你是否尝试配置NUnit从VS(作为外部程序)运行它?这样你可以让NUint运行你的测试。在调试器下

+0

我在外部运行NUnit,但在VS内部使用了Resharper的单元测试运行器。两者都没有调试失败,但步进虽然工作得很好。 – 2009-12-14 09:34:10