I've got this asynchronous method that I try to call before my tests run (to make sure the database is cleaned out) and then again after each test:
private async Task DeleteTestExtensions()
{
var authorization = _extensionRepository.GetAuthorizationByTaxAccountIdAndYear(_testTaxAccountId, _testTaxAccountYear);
var extensions = await _extensionRepository.GetExtensionsByUserIdAsync(_testUserId);
if (extensions.Count > 0) {
foreach (var extension in extensions)
{
try
{
await _extensionRepository.DeleteExtensionAsync(extension.ExtensionID);
}
catch (Exception) { }
}
}
try
{
_extensionRepository.DeleteAuthorization(authorization.ExtensionAuthorizationID);
}
catch (Exception) { }
}
In my [TestInitialize]
, I'm calling it via DeleteTestExtensions().Wait()
, since I apparently can't use await
inside of a non-asynchronous method, and I can't make my Test Initialization asynchronous.
However, whenever I execute a test, I get a System.AggregateException with a System.NullReferenceException, however I have verified that all parameters are valid. It is likely that both of the method calls at the top will return empty, which I imagine would be fine based on the if statement and the try-catches.
So why would I be getting this exception?
EDIT: Here's the stack trace from the exception:
System.AggregateException was unhandled by user code
HResult=-2146233088
Message=One or more errors occurred.
Source=mscorlib
StackTrace:
at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
at System.Threading.Tasks.Task.Wait()
at KC.eFile.DataAccess.Tests.Repositories.ExtensionRepositoryTests.Initialization() in C:\Users\rakibler\Projects\new_efile_server\Keystone E-File\KC.eFile.DataAccess.Tests\Repositories\ExtensionRepositoryTests.cs:line 58
InnerException:
HResult=-2147467261
Message=Object reference not set to an instance of an object.
Source=KC.eFile.DataAccess.Tests
StackTrace:
at KC.eFile.DataAccess.Tests.Repositories.ExtensionRepositoryTests.<DeleteTestExtensions>d__8.MoveNext() in C:\Users\rakibler\Projects\new_efile_server\Keystone E-File\KC.eFile.DataAccess.Tests\Repositories\ExtensionRepositoryTests.cs:line 71
InnerException: