我想测试控制器的动作,但有一点不被visual studio代码覆盖工具覆盖。单元测试asp.net mvc控制器动作tryupdate
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");***
}
代码覆盖率
,*返回查看( “PageNotFound”); *未被覆盖。 因为,我无法模拟TryUpdateModel错误stuStation。如果模型无法更新,TryUpdateModel可能会变为false。你能帮忙吗?
说实话,我不认为你需要提供该场景的单元测试。我认为它应该是你的代码调试和测试的一部分。我可能会在该示例中单元测试的唯一方法是GetCityById() – dreza