2014-01-06 113 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 
}