首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >与IndexOutOfRangeException相关的问题

与IndexOutOfRangeException相关的问题
EN

Stack Overflow用户
提问于 2011-07-11 01:08:56
回答 1查看 514关注 0票数 1

我在我的c#代码中得到了IndexOutOfRangeException的连续错误。代码片段如下:

代码语言:javascript
运行
复制
public void GetAccountSalesDataTestWithAccountsIncluded()
{
    AccountSalesDataRepository target = new AccountSalesDataRepository();
    AccountSalesDataSearchCriteria[] searchCriteria = new AccountSalesDataSearchCriteria[2]
     {   
        new AccountSalesDataSearchCriteria
        {
             ProgramAccountId = new AccountSalesDataSearchCriteria.SearchCriteria<int>[1] { new AccountSalesDataSearchCriteria.SearchCriteria<int>(98, true) }
        },
        new AccountSalesDataSearchCriteria()
     };

    AccountSalesDataSummary[] results;
    results = target.GetAggregateAccountSalesData(searchCriteria, true);
    try
    {
        Assert.IsNotNull(results, "The result set should not be null with the given account");
        Assert.IsTrue(results.Length > 0, "The result set should not be empty with given account");
    }
    catch (AssertFailedException /*ex*/)
    {
    }
    this.AccountSalesDataSummaryBasicTest(results, true);
    try
    {
        Assert.AreEqual(results[0].AccountId, 2);
        Assert.AreEqual(results[0].TotalPurchaseAmount, decimal.Parse("200"), "The total purchase amount is incorrect");
        Assert.AreEqual(results[0].TotalPurchaseQuantity, 2000, "The total purchase quantity is incorrect");
        Assert.AreEqual(results[0].TotalSaleQuantity, double.Parse("200"), "The total sales quantity is incorrect");
        Assert.AreEqual(results[0].TotalSalesAmount, decimal.Parse("20"), "The total sales amount is incorrect");
    }
    catch (AssertFailedException /*ex*/)
    {
    }
}

可能的原因是什么?

请允许我认为我的概念不连贯,因为我对这整个事情真的很陌生。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-07-11 01:15:03

您显然是在编写单元测试。AssertFailedException表明您的某个断言失败了,不应该捕获它,因为整个要点是,如果断言失败,您的整个测试就会失败(继续测试没有意义,因为您已经知道有些东西是错的)。此外,当您捕捉到异常而在catch块中不做任何事情时,您实际上是在说“如果抛出了异常,就忽略它并继续”。因此,本应检查数组是否包含某些内容的断言完成了它的任务,但是您将其静默并使测试继续进行,即使数组为空-因此下一个try块中的IndexOutOfRangeException

删除try/catch块(保留try块的内容),您将看到测试失败,并告诉您确切的错误所在:数组为空。它为空的原因要么是GetAggregateAccountSalesData()中存在错误(很好,测试帮助您找到了错误的错误),要么是您调用它的方法不正确,或者是测试数据丢失(是否存在可以聚合的帐户销售数据?),或者是某些东西设置得不正确(为了让GetAggregateAccountSalesData()工作,您需要调用其他一些方法吗?)尝试调试测试,看看该方法内部发生了什么。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6642538

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档