2014-01-06 152 views
0

我工作的單元測試對我的MVC 4 application.And下面是我想要做單元測試方法之一: -單元測試方法根據型號

[HttpPost] 
    public ActionResult Index(ProductViewModel model) 
    { 
     if (model != null) 
     { 

      return PartialView("_ProductGrid", SearchProduct(model)); 
     } 
     else 
     { 
      return RedirectToAction("Index"); 
     } 
    } 

我寫的單元測試方法,但是當我通過代碼覆蓋選項檢查代碼覆蓋時,其他部分顯示爲未被覆蓋。但我不確定原因。

任何人都可以幫我解決這個問題嗎?

下面是我的測試方法的代碼:

[TestMethod] 
    public void IndexPostTest() 
    { 
     // Arrange 
     const string searchInDescription = "all"; 

     ProductController controller = new ProductController(); 
     ProductViewModel model = new ProductViewModel 
     { 
      SearchA = true, 
      SearchB= true, 
      SearchIC = true, 
      Description = searchInDescription 
     }; 

     TestControllerBuilder builder = new TestControllerBuilder(); 
     builder.InitializeController(controller); 

     // Act   
     var result = controller.Index(model) as ActionResult; 

     var viewmodel = (ProductViewModel)((ViewResultBase)(result)).Model; 

     int matches = _productService.LookupA("", searchInDescription).Count + 
         _productService.LookupB("", searchInDescription).Count + 
         _ProductService.LookupC("", searchInDescription).Count; 

     if (result != null && viewmodel != null && result.GetType() == typeof(PartialViewResult)) 
     { 
      // Assert 
      Assert.IsNotNull(result); 
      Assert.IsInstanceOfType(viewmodel, typeof(ProductViewModel)); 

      if (viewmodel.Products != null) 
       Assert.AreEqual(matches, viewmodel.Products.Count()); 
      if (matches > 0 && viewmodel.Products != null && viewmodel.Products.ToList().Count > 0 && viewmodel.Products.ToList()[0].Description != "") 
      { 
       Assert.IsTrue(viewmodel.Products.ToList()[0].Description.ToUpper().Contains(searchInDescription.ToUpper())); 
      } 
     } 
     else if (result != null && result.GetType() == typeof(RedirectResult)) 
     { 
      var redirectResult = result as RedirectResult; 
      // Assert 
      Assert.IsNotNull(result); 
      Assert.AreEqual("Index", redirectResult.Url); 

     } 
    } 
+2

你能顯示你的單元測試的其他部分? –

回答

2

不要在您的測試中使用條件邏輯。決不。測試應該在多次運行時返回相同的結果,並且應該驗證單個事物。所以,你實際上應該有兩個測試 - 一個在模型有效時驗證案例(你有這個測試),另一個在模型無效時驗證案例(你沒有那個測試,因爲你提供了有效的模型)。

第一個測試是爲有效模式:

[TestMethod] 
public void ShouldProvideProductGridWhenModelIsNotNull() 
{ 
    // Arrange   
    ProductController controller = new ProductController(); 
    ProductViewModel model = new ProductViewModel 
    { 
     SearchA = true, 
     SearchB= true, 
     SearchIC = true, 
     Description = searchInDescription 
    }; 

    // ... other arrange code 

    // Act   
    var result = (PartialViewResult)controller.Index(model); 

    // Assert 
    Assert.IsNotNull(result); 
    var viewmodel = (ProductViewModel)result.Model; 
    // ... other assertions 
} 

和第二測試重定向:

[TestMethod] 
public void ShouldRedirectToIndexWhenModelIsNull() 
{ 
    // Arrange   
    ProductController controller = new ProductController();  

    // ... other arrange code 

    // Act   
    var result = (RedirectToActionResult)controller.Index(null); 

    // Assert 
    Assert.IsNotNull(result); 
    Assert.AreEqual("Index", redirectResult.Url); 
} 
+0

感謝它現在的工作。 – Pawan

2

你永遠只能測試Index行動,模型,它不爲空。因此Index操作的else部分將永遠不會從您的測試中調用。

您需要兩個測試。一個用於測試模型何時爲null的測試,另一個測試是否爲null。

[TestMethod] 
public void IndexPost_NotNull() 
{ 
    // TODO: Setup test data as you have done 

    // Act   
    var result = controller.Index(model) as ActionResult; 

    // Assert 
    // TODO: check result is a partial view result 
} 

[TestMethod] 
public void IndexPost_Null() 
{ 
    // Act   
    var result = controller.Index(null) as ActionResult; 

    // Assert 
    // TODO: check result is a redirect result 
}