C# 与IndexOutOfRangeException相关的问题

C# 与IndexOutOfRangeException相关的问题,c#,C#,我在我的c#代码中得到了IndexOutOfRangeException的连续错误。代码片段如下所示: public void GetAccountSalesDataTestWithAccountsIncluded() { AccountSalesDataRepository target = new AccountSalesDataRepository(); AccountSalesDataSearchCriteria[] searchCriteria = new Accoun

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

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*/)
    {
    }
}
public void GetAccountSalesDataTestWithAccountsIncluded()
{
AccountSalesDataRepository目标=新AccountSalesDataRepository();
AccountSalesDataSearchCriteria[]searchCriteria=新AccountSalesDataSearchCriteria[2]
{   
新AccountSalesDataSearchCriteria
{
ProgramAccountId=new AccountSalesDataSearchCriteria.SearchCriteria[1]{new AccountSalesDataSearchCriteria.SearchCriteria(98,true)}
},
新AccountSalesDataSearchCriteria()
};
AccountSalesDataSummary[]结果;
结果=target.GetAggregateAccountSalesData(searchCriteria,true);
尝试
{
IsNotNull(results,“给定帐户的结果集不应为null”);
Assert.IsTrue(results.Length>0,“对于给定帐户,结果集不应为空”);
}
捕获(AssertFailedException/*ex*/)
{
}
此.AccountSalesDataSummaryBasicTest(结果,true);
尝试
{
Assert.AreEqual(结果[0].AccountId,2);
Assert.AreEqual(结果[0].TotalPurchaseAmount,decimal.Parse(“200”),“采购总额不正确”);
Assert.AreEqual(结果[0].TotalPurchaseQuantity,2000,“总采购数量不正确”);
Assert.AreEqual(结果[0].TotalSaleQuantity,double.Parse(“200”),“总销售数量不正确”);
Assert.AreEqual(结果[0]。TotalSalesAmount,decimal.Parse(“20”),“总销售额不正确”);
}
捕获(AssertFailedException/*ex*/)
{
}
}
可能的原因是什么


请原谅,我可能会说我与我的概念不一致,因为我对这整件事真的很陌生。

你显然是在写单元测试。
AssertFailedException
表示您的一个断言失败了,不应该捕获它,因为关键是如果一个断言失败,您的整个测试应该失败(继续测试没有意义,因为您已经知道出了问题)。此外,当您捕获异常并且不在
catch
块中执行任何操作时,您实际上是在说“如果抛出异常,请忽略它并继续”。因此,应该检查数组是否确实包含某些内容的断言完成了它的工作,但您使其静音,并使测试继续进行,即使数组为空-因此在下一个
try
块中出现
IndexOutOfRangeException


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

您显然是在编写单元测试。
AssertFailedException
表示您的一个断言失败,不应该捕获它,因为关键是如果断言失败,整个测试都应该失败(继续测试没有意义,因为您已经知道有问题)。此外,当您捕获异常并且在
catch
块中不执行任何操作时,您实际上是在说“如果抛出异常,请忽略它并继续”。因此,本应检查数组是否确实包含某些内容的断言完成了其工作,但您使其静音,并使测试继续进行,即使数组为空-因此在下一个
try
块中出现
IndexOutOfRangeException


删除
try
/
catch
块(保留
try
块的内容),您将看到测试失败,并准确地告诉您错误:数组为空。它为空的原因可能是
GetAggregateAccountSalesData()
中存在错误(很好,测试帮助您找到了错误),或者您错误地调用了它,或者缺少测试数据(是否存在可以聚合的account sales数据?),或者某些设置不正确(是否需要调用其他一些方法才能使
GetAggregateAccountSalesData()
正常工作?)尝试调试测试并查看该方法内部发生的情况。

异常在哪一行引发。Assert.AreEqual(结果[0]。AccountId,2);所有Assert.AreEqual语句都给出相同的错误。从
Assert.IsTrue(结果.Count>0)开始如何
?事实上,我的结果集是空的。
目标.GetAggregateAccountSalesData(searchCriteria,true)
返回的是空集。我关闭它是因为太本地化了。您的问题出现了(正如您在上面所评论的),因为我们没有可见性的方法没有返回任何数据。异常在哪一行引发。Assert.AreEqual(results[0].AccountId,2);所有assert.areequal语句都给出了相同的错误。从一个
assert.IsTrue(results.Count>0)
开始怎么样?实际上我的结果集是空的。
target.GetAggregateAccountSalesData(searchCriteria,true)
正在返回空集。我正在关闭它,因为太本地化了。您的问题出现了(正如您在上面所评论的)因为我们无法看到的一个方法没有返回任何数据。谢谢您的帮助。它起作用了。@user837610:很高兴听到它。如果您对答案感到满意,您应该将它标记为“已接受”以表示接受