Unit testing 单元测试是一种基于模型的方法
我正在MVC 4应用程序上进行单元测试。下面是我想进行单元测试的方法之一:-Unit testing 单元测试是一种基于模型的方法,unit-testing,asp.net-mvc-4,Unit Testing,Asp.net Mvc 4,我正在MVC 4应用程序上进行单元测试。下面是我想进行单元测试的方法之一:- [HttpPost] public ActionResult Index(ProductViewModel model) { if (model != null) { return PartialView("_ProductGrid", SearchProduct(model)); } else
[HttpPost]
public ActionResult Index(ProductViewModel model)
{
if (model != null)
{
return PartialView("_ProductGrid", SearchProduct(model));
}
else
{
return RedirectToAction("Index");
}
}
我已经为此编写了单元测试方法,但是当我通过代码覆盖率选项检查它的代码覆盖率时,else部分显示为uncovered。但是我不确定原因
有人能帮我吗
以下是我的测试方法代码:
[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);
}
}
不要在测试中使用条件逻辑。从未。当您多次运行该测试时,它应该返回相同的结果,并且它应该验证一件事情。所以,您实际上应该有两个测试——一个在模型有效时验证用例(您有那个测试),另一个在模型无效时验证用例(您没有那个测试,因为您提供了有效的模型) 第一个测试是针对有效模型:
[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);
}
不要在测试中使用条件逻辑。从未。当您多次运行该测试时,它应该返回相同的结果,并且它应该验证一件事情。所以,您实际上应该有两个测试——一个在模型有效时验证用例(您有那个测试),另一个在模型无效时验证用例(您没有那个测试,因为您提供了有效的模型) 第一个测试是针对有效模型:
[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);
}
您只能使用非空的模型测试
索引操作。因此,Index
操作的else
部分将永远不会从测试中调用
你需要做两次测试。一个测试模型何时为空,另一个测试模型何时为空
[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
}
您只能使用非空的模型测试索引操作。因此,Index
操作的else
部分将永远不会从测试中调用
你需要做两次测试。一个测试模型何时为空,另一个测试模型何时为空
[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
}
你能为其他部分展示你的单元测试吗?你能为其他部分展示你的单元测试吗?