I am getting the continuous error of IndexOutOfRangeException in my c# code. The code snippet is follows:
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*/)
{
}
}
What can be the probable reason of this?
Please pardon me if I may give the idea that I am not coherent with my concepts, because I’m really new to this entire thing.
You are apparently writing unit tests. The
AssertFailedExceptionindicates that one of your assertions has failed, and it should not be caught, because the entire point is that if an assertion fails, your entire test is supposed to fail (there is no point in continuing the test, because you already know that something is wrong). Furthermore, when you catch an exception and don’t do anything in thecatchblock, you’re effectively saying “if an exception is thrown, just ignore it and proceed”. Thus, the assert that is supposed to check that the array actually contains something did its job, but you silenced it and made the test proceed even though the array is empty – hence theIndexOutOfRangeExceptionin the nexttryblock.Remove the
try/catchblocks (keeping the content of thetryblocks), and you will see your test fail and tell you exactly what’s wrong: the array is empty. The reason it is empty is either that there is a bug inGetAggregateAccountSalesData()(excellent, the test helped you find a bug), or that you have called it incorrectly, or that test data is missing (does there exist account sales data that can be aggregated?), or that something is somehow not set up properly (do you need to call some other methods in order forGetAggregateAccountSalesData()to work?) Try debugging the test and see what happens inside that method.