2009-12-11 46 views
1

我一直在構建一個擴展庫,並且我在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歲的男性)。

有什麼想法?

回答

0

你是否嘗試配置NUnit從VS(作爲外部程序)運行它?這樣你可以讓NUint運行你的測試。在調試器下

+0

我在外部運行NUnit,但在VS內部使用了Resharper的單元測試運行器。兩者都沒有調試失敗,但步進雖然工作得很好。 – 2009-12-14 09:34:10

相關問題