如何在 Web API ASP.Net core 中对异常过滤器进行单元测试。不想模拟 onException 方法。我在单元测试中像下面这样 mock 了 ControllerContext。从 Web API 调用的类库会抛出自定义异常,这些异常会被异常过滤器捕获,我想对其进行单元测试。今天我收到“值未落在预期范围内”错误。
public static ControllerContext GetMockControllerContext(
this Controller controller,
RouteData routeData)
{
var actionContext = new ActionContext
{
HttpContext = new DefaultHttpContext
{
User = controller.GetMockUserContext()
},
RouteData = routeData,
ActionDescriptor = new ControllerActionDescriptor()
};
return new ControllerContext(actionContext);
}
最佳答案
我将使用 TestServer 编写集成测试,如 documentation 中所述。 。它的运行速度与实际的单元测试一样快,但更有值(value),因为它将演示您的过滤器在实际的 MVC 上下文/管道中工作。
关于asp.net-mvc - 如何在 Web API ASP.Net core 中对异常过滤器进行单元测试。不想模拟 onException 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42349405/