2017-03-03 22 views
0

我有一個OData的啓用的WebAPI包含如何拋出並得到異常對象中最小起訂量的WebAPI

  1. 的TestController:它包含了一個自定義操作方法。
  2. WebApiExceptionHandler:WebApi中的自定義異常處理程序, 在WebApiConfig.cs中註冊。
  3. ITestManager:接口
  4. 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; 
    } 
} 
+0

有許多不必要的信息可以提供。請閱讀[如何創建最小,完整和可驗證示例](http://stackoverflow.com/help/mcve)。 –

+0

這將需要一個集成測試,其中所有的移動部件都配置好了,而不是像你目前試圖實現的單元測試 – Nkosi

+0

@SergeyShushlyapin我不好意思提供了很多信息。正確指出您的觀點。 – Quest

回答

1

取出[ExpectedException]測試方法屬性(違反AAA原則)和重寫你的測試使用Assert.Throws斷言節NUnit方法。

+0

使用NUnit就像魅力一樣。但我仍然陷入了一個問題。我發現很難理解的是,我使用ExceptionHandler來處理API中的所有異常。當我使用MoQ「iTestManagerMock.Setup(i => i.UpdateTestQuantity()。拋出(新的Exception());顯式拋出異常,那麼爲什麼控制永遠不會到達返回Exception對象的ExceptionHandler類。 ,我需要看到由ExceptionHandler返回的那個對象來做Assert。 – Quest

+0

因爲這個,我真的被我的工作困住了。 – Quest