【发布时间】:2012-09-09 18:06:11
【问题描述】:
我想测试控制器动作,但是Visual Studio代码覆盖工具没有覆盖一个点。
public ActionResult Activate(int? id)
{
if (id == null)
return View("PageNotFound");
var city = repository.GetCityById(id.Value);
if (city == null)
return View("PageNotFound");
city.IsActive = !city.IsActive;
if (TryUpdateModel(city))
{
repository.Save();
return RedirectToAction("MyCities");
}
***return View("PageNotFound");***
}
在代码覆盖中,*return View("PageNotFound");* 没有被覆盖。 因为,我无法模拟 TryUpdateModel 错误的情况。如果模型无法更新,TryUpdateModel 可能会为假。你能帮忙吗?
【问题讨论】:
-
老实说,我认为您不需要为该场景提供单元测试。我认为它应该是您编码调试和测试的一部分。在该示例中我可能会进行单元测试的唯一方法是 GetCityById()
标签: c# asp.net-mvc-3 unit-testing tdd code-coverage