使用模拟测试控制器

时间:2016-06-15 17:27:05

标签: asp.net-mvc unit-testing mocking

这是我学习和进行单元测试的第二天。我的团队使用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);
    }

0 个答案:

没有答案