There is an action method that registers the user. In order not to clutter up the model, the password confirmation was issued as a separate field in the view. As a result, I get this value through Request.Form["Повторите пароль"]

Part of the action method:

 if (user.Password == Request.Form["Повторите пароль"] && ModelState.IsValid) { TempData["Message"] = "Регистрация прошла успешно! Можете логиниться"; repository.Add_New_User(user); return Redirect("/Account/Login"); } 

And so I began to write a test and how do I set this Request.Form :

  [TestMethod] public void Registr_Error( { User testUser = new User() { LoginUser = "IlyaLogin", Password = "IlyaPassword" }; Mock<IUsersRepository> mock = new Mock<IUsersRepository>(); mock.Setup(m => m.Users).Returns(new User[] { new User { LoginUser = "Vova", Password="123123"}, testUser }.AsQueryable()); AccountController target = new AccountController(mock.Object, null); ViewResult result = (ViewResult)target.Registr(testUser, null); Assert.AreEqual("Registr", result.ViewName); Assert.IsFalse(result.ViewData.ModelState.IsValid); } 

    1 answer 1

      Mock<HttpRequestBase> request = new Mock<HttpRequestBase>(); request .Setup(request => request.Form) .Returns(new FormCollection { { "Повторите пароль", "Password" } }); Mock<HttpContextBase> context = new Mock<HttpContextBase>(); context .SetupGet(c => c.Request) .Returns(request.Object); Controller controller = new Controller(); // System Under Test controller.ControllerContext = new ControllerContext(context.Object, new RouteData(), controller); 
    • Artem, can you give your mail for communication? Just just learning to work with MVC and I would like for someone to look at my little project, because all of a sudden I made so many crutches there and I don't know about them ... - batya