我有一個OData的啓用的WebAPI包含如何拋出並得到異常對象中最小起訂量的WebAPI
- 的TestController:它包含了一個自定義操作方法。
- WebApiExceptionHandler:WebApi中的自定義異常處理程序, 在WebApiConfig.cs中註冊。
- ITestManager:接口
TestManager:實現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;
}
}
有許多不必要的信息可以提供。請閱讀[如何創建最小,完整和可驗證示例](http://stackoverflow.com/help/mcve)。 –
這將需要一個集成測試,其中所有的移動部件都配置好了,而不是像你目前試圖實現的單元測試 – Nkosi
@SergeyShushlyapin我不好意思提供了很多信息。正確指出您的觀點。 – Quest