我有一个包含
的支持OData的WebAPITestManager:实现ITestManager的类。这个类处理 数据的所有业务逻辑。 WebAPI控制器TestController 有一个函数可以调用这个类。
此外,还有一个TestWebAPI项目,它使用MoQ框架进行测试。 TestMethod“Test_UpdatePackageVendorOnException__Success”使用MoQ设置在UpdateTestQuantity方法中抛出Exception。但是当我调试此测试方法时,抛出异常时,调试器不会指向WebApiExceptionHandler类。理想情况下,API中发生的所有异常都在WebApiExceptionHandler类
我面临的问题是,在我的测试方法中,我想测试WebApiExceptionHandler返回的异常对象。但由于控件永远不会出现在WebApiExceptionHandler中,所以我不能这样做。
如何修改代码,以便在TestMethod中我可以测试WebApiExceptionHandler返回的自定义对象。
WebAPI代码:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Services.Replace(typeof(IExceptionHandler), new WebApiExceptionHandler());
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "DefaultApiGetParam",
routeTemplate: "api/{controller}/{key}/{count}",
defaults: new { key = RouteParameter.Optional, count = RouteParameter.Optional }
);
}
}
public class WebApiExceptionHandler : ExceptionHandler
{
public WebApiExceptionHandler()
{
}
public override void Handle(ExceptionHandlerContext context)
{
try
{
var objCustomException = new CustomException(context.Exception);
context.Result = new ResponseMessageResult(context.Request.CreateResponse(HttpStatusCode.InternalServerError, objCustomException);
}
catch (Exception)
{
base.Handle(context);
}
}
public override bool ShouldHandle(ExceptionHandlerContext context)
{
return true;
}
}
public interface ITestManager
{
bool UpdateTestQuantity();
}
public class TestManager : ITestManager
{
public TestManager()
{
}
public bool UpdateTestQuantity()
{
//Custom Logic
return true;
}
}
public class TestController : ODataController
{
ITestManager testManager;
public TestController()
{
testManager = new TestManager();
}
public TestController(ITestManager iTestManager)
{
testManager = iTestManager;
}
public IHttpActionResult UpdateTestQuantity(ODataActionParameters param)
{
bool result = testManager.UpdateTestQuantity();
return Ok();
}
}
WebApiTest项目
[TestClass]
public class TestControllerTest
{
Mock<ITestManager> iTestManagerMock;
TestController objTestController;
[TestInitialize]
public void Setup()
{
iTestManagerMock = new Mock<ITestManager>();
}
[TestMethod]
[ExpectedException(typeof(Exception), AllowDerivedTypes = true)]
public void Test_UpdateTestQuantityOnException__Success()
{
iTestManagerMock.Setup(i => i.UpdateTestQuantity().Throws(new Exception());
objTestController = new TestController(iTestManagerMock.Object);
var objODataActionParameters = new ODataActionParameters();
objTestController.Request = new HttpRequestMessage();
objTestController.Configuration = new HttpConfiguration();
IHttpActionResult objIHttpActionResult = objTestController.UpdateTestQuantity(objODataActionParameters);
//Problem Area
//Control never reaches here after the exception is thrown
//Want to check some property of exception object returned via Exception WebApiExceptionHandler
}
[TestCleanup]
public void Clean()
{
iTestManagerMock = null;
}
}