这是我学习和进行单元测试的第二天。我的团队使用NSubstitute
。
这是我编写的动作方法,现在我必须为它编写一些单元测试。我不知道如何去做?我们嘲笑controller
吗?然后我们模拟一个GET
方法?怎么样 ?我们如何测试它是否发布了什么并且是POST
?等...
public ActionResult ForgotPassword(string button, ForgotPasswordViewModel fpModel = null)
{
string method = HttpContext.Request.HttpMethod;
if (method == "GET")
{
ViewBag.Status = "CREATE_TASK";
ForgotPasswordViewModel model = _forgotPasswordManager.LoadForgotPasswordSettings();
ModelState.Clear();
if (model != null && !string.IsNullOrWhiteSpace(model.ForgotPasswordMethod) && model.ForgotPasswordMethod.Trim().ToUpper() == "TASKS")
return View(model);
}
if (method == "POST")
{
// cancel button, go back to LoginPage
if (!string.IsNullOrEmpty(button) && button == "cancel")
{
return RedirectToAction("Login");
}
else
{
if (this.CreateTask(fpModel))
{
ViewBag.Status = "TASK_CREATED";
}
else
{
fpModel = new ForgotPasswordViewModel();
ViewBag.Status = "CREATE_TASK";
return View(fpModel);
}
}
}
return View(fpModel);
}