2009-12-11 3 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에 소요되며, 사전에 항목을 선회하고, 사용자가 정의하는 어떤 기능을 사용하여 항목을 집계합니다. 필자의 샘플 데이터 세트는 사람 (사람이라고 불리는 유형)의 배열이다.

이 테스트의 경우에 반환되는 내용
/// <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."); 
    } 

가하는 사전> 인스턴스 :

 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 } 
     }; 

그리고 마지막으로, 우리는 우리의 시험을한다. 부울은 IsMale 그룹의 결과이며 샘플 데이터에서는 true와 false의 2 개 항목을 올바르게 반환합니다. 내부 사전은 연령의 키와 카운트의 값을가집니다. 우리의 테스트 데이터에서 조직 된 [true] [30]은 세트의 30 세 남성을 모두 나타냅니다.

문제는 피벗 함수 자체는 아니지만, 어떤 이유로 NUnit 테스트 러너와 Resharper의 Unit Test Runner를 통해이 테스트를 실행하면 테스트가 실패하고 "int count = organized"행에 대해 KeyNotFoundException이보고됩니다 [true] [30]; " 이 테스트를 디버그하면 값 3을 올바르게 반환합니다 (예제 데이터에서와 같이 30 세의 남성이 3 명 있습니다).

의견이 있으십니까?

답변

0

VS 내에서 외부 프로그램으로 실행하려면 NUnit을 구성하려고 했습니까? 이렇게하면 NUint가 테스트를 실행할 수있게됩니다. 디버거에서

+0

외부에서 NUnit을 실행했지만 VS에서 내부적으로 Resharper의 Unit Test Runner를 사용했습니다. 두 가지 모두 디버깅없이 실패하지만 스테핑은 정상적으로 작동합니다. –

관련 문제