KeyNotFoundException,但不是在调试时

时间:2009-12-11 13:09:31

标签: c# dictionary nunit pivot keynotfoundexception

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

    /// <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 = "matthew@somewhere.com", Age = 25, IsMale = true },
            new Person { Forename = "Chris", Surname = "Someone", Email = "chris@somewhere.com", Age = 28, IsMale = false },
            new Person { Forename = "Andy", Surname = "Someone", Email = "andy@somewhere.com", Age = 30, IsMale = true },
            new Person { Forename = "Joel", Surname = "Someone", Email = "joel@somewhere.com", Age = 30, IsMale = true },
            new Person { Forename = "Paul", Surname = "Someone", Email = "paul@somewhere.com", 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.");
    }

此测试用例中返回的内容是Dictionary&gt;实例。布尔值是IsMale组的结果,在我们的示例数据中,正确返回2个项,true和false。内部字典具有年龄的关键字和计数值。在我们的测试数据中,有组织的[true] [30]反映了该组中所有30岁的男性。

问题不在于枢轴函数本身,但由于某种原因,当我们通过NUnit Test Runner和Resharper的Unit Test Runner运行时,测试失败,报告“Int count = organized [”行的KeyNotFoundException真] [30];”。当我们调试此测试时,它会正确返回值3(如我们的示例数据中,我们有3个年龄为30岁的男性)。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

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