0

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: 
Alex Kibler
  • 4,674
  • 9
  • 44
  • 74
  • Can you post the full stack trace of inner exception of AggregateException? – vendettamit Dec 17 '15 at 16:50
  • Are you iterating and removing elements from same Collection? – Valentin Dec 17 '15 at 16:51
  • @Valentin not technically I don't think. the Delete method is deleting a record from the DB with the same ID as the item in the collection, but the collection remains untouched at the end. – Alex Kibler Dec 17 '15 at 17:44
  • You need to get to a point where you can ask a more specific question than "why would I be getting this [i.e. `NullReferenceException`] exception?" If you get to that point and still need help, post a new question in which you provide a good [mcve] that reliably reproduces the problem, along with a detailed description of what you've done to debug the problem already and what _specifically_ you need help with. – Peter Duniho Dec 17 '15 at 21:08

0 Answers0