Assert.ThrowsException test fails when it does throw exception

6.5k Views Asked by At

I'm using Microsoft.VisualStudio.TestTools.UnitTesting for unit testing.

I have this test which asserts that an exception was thrown and it is failing

[TestMethod]
public async Task ShouldThrowHttpExceptionIfPassedObjectIsNull()
{
    _mockLogger = new Mock<ILogger<DataService>>();

    // setup memoryCache
    object expected = null;
    _mockNullMemoryCache = MockMemoryCacheService.GetNullMemoryCache(expected);
    _mockSessionManagementService = new MockSessionManagementService();

    _ds = new DataService(
        _mockLogger.Object,
        _mockNullMemoryCache.Object,
        _mockSessionManagementService
    );
    Assert.ThrowsException<HttpException>(() => _ds.RetrieveFileData<object>(_incorrectFilePath, false));
}

When I debug it, the last line of code to run before it errors out and fails is this:

enter image description here

The exception ex is a system.invalidOperationException but that shouldn't matter because it throws an HttpException not matter what. Why is the test failing with the reason:

Test Name: ShouldThrowHttpExceptionIfPassedObjectIsNull Test FullName: xxx.xxx.xxx.xxx.ShouldThrowHttpExceptionIfPassedObjectIsNull Test Source: C:\Users\xxx\xxx.cs : line 88 Test Outcome: Failed Test Duration: 0:04:35.5251661

Result StackTrace: at xxxxxx.d__10.MoveNext() in C:\Users\xxx\xxx\xxx.cs:line 101 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) Result Message: Assert.ThrowsException failed. No exception thrown. HttpException exception was expected.

EDIT The code then moves into this code block after the image:

else
                {
                    // Parsing issue
                    _logger.LogError(string.Format("Id={0}, Could not retrieve data from {1} : Malformed source data", _sessionManagementService.SessionId, url));
                    throw new HttpException(HttpStatusCode.InternalServerError, "Malformed source data", true);
                }

As it is throwing the HttpException above, it throws a DllNotFoundException.

Exception thrown: 'System.DllNotFoundException' in System.Private.CoreLib.ni.dll

Additional information: Unable to load DLL 'combase.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)

1

There are 1 best solutions below

5
On BEST ANSWER

When I look at the type hierarchy of HttpException, I see that it doe not inherit from InvalidOperationException. Therefore the unit test fails correctly.

Likely there's some LINQ code or other code which cannot execute correctly, therefore catches the HttpException and converts it into the InvalidOperationException.

You should be able to debug the unit test. Instruct Visual Studio to stop at exceptions, then go forward step by step and try to find out where your exception gets converted, e.g. when you step through some LINQ code.

The MoveNext() call indicates that something tries to loop over values. When debugging, disable Just my code, because it could be in the .NET framework.