Assert.ThrowsException 抛出异常时测试失败
Assert.ThrowsException test fails when it does throw exception
我正在使用 Microsoft.VisualStudio.TestTools.UnitTesting
进行单元测试。
我有这个测试断言抛出异常并且它失败了
[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));
}
当我调试它时,运行 在它出错和失败之前的最后一行代码是这样的:
异常 ex
是 system.invalidOperationException
但这无关紧要,因为它抛出 HttpException 无关紧要。为什么测试失败,原因是:
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.
编辑
然后代码在图像之后移动到这个代码块中:
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);
}
因为它抛出了上面的 HttpException,所以它抛出了 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)
当我查看 HttpException 的类型层次结构时,我发现它没有继承自 InvalidOperationException。因此单元测试正确失败。
可能有一些 LINQ 代码或其他代码无法正确执行,因此捕获 HttpException 并将其转换为 InvalidOperationException。
您应该能够调试单元测试。指示 Visual Studio 在异常处停止,然后逐步前进并尝试找出异常被转换的位置,例如当您单步执行某些 LINQ 代码时。
MoveNext() 调用表明某些东西试图循环值。调试时,禁用Just my code,因为它可能在.NET框架中。
我正在使用 Microsoft.VisualStudio.TestTools.UnitTesting
进行单元测试。
我有这个测试断言抛出异常并且它失败了
[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));
}
当我调试它时,运行 在它出错和失败之前的最后一行代码是这样的:
异常 ex
是 system.invalidOperationException
但这无关紧要,因为它抛出 HttpException 无关紧要。为什么测试失败,原因是:
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.
编辑 然后代码在图像之后移动到这个代码块中:
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);
}
因为它抛出了上面的 HttpException,所以它抛出了 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)
当我查看 HttpException 的类型层次结构时,我发现它没有继承自 InvalidOperationException。因此单元测试正确失败。
可能有一些 LINQ 代码或其他代码无法正确执行,因此捕获 HttpException 并将其转换为 InvalidOperationException。
您应该能够调试单元测试。指示 Visual Studio 在异常处停止,然后逐步前进并尝试找出异常被转换的位置,例如当您单步执行某些 LINQ 代码时。
MoveNext() 调用表明某些东西试图循环值。调试时,禁用Just my code,因为它可能在.NET框架中。