【发布时间】:2017-02-23 19:31:27
【问题描述】:
我正在使用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。测试失败的原因:
测试名称:ShouldThrowHttpExceptionIfPassedObjectIsNull 测试 全名:xxx.xxx.xxx.xxx.ShouldThrowHttpExceptionIfPassedObjectIsNull 测试 来源:C:\Users\xxx\xxx.cs :第 88 行测试结果:失败测试持续时间:0:04:35.5251661
结果堆栈跟踪:在 xxxxxx.d__10.MoveNext() 在 C:\Users\xxx\xxx\xxx.cs:line 101 --- 从先前抛出异常的位置结束堆栈跟踪 --- 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务 任务)在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)结果消息:Assert.ThrowsException 失败。没有例外 抛出。预期会出现 HttpException 异常。
编辑 然后代码在图像之后移动到这个代码块中:
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。
抛出异常:“System.DllNotFoundException”在 System.Private.CoreLib.ni.dll
附加信息:无法加载 DLL 'combase.dll': 找不到指定的模块。 (HRESULT 的例外情况: 0x8007007E)
【问题讨论】:
-
@ThomasWeller 否,但测试正在测试 HttpException,这是抛出的异常。
标签: c# unit-testing visualstudio.testtools