How to taunt with MVK POST form with Moq

Can anyone tell me how to mock the MVC form post using Moq?

All I want is a unit test my method based on several different form posts.

I tried Google for this, and there is no top-down guide.

thanks

EDIT: Adding Code

[TestMethod] public void SubscriptionControllerTest() { var subscriptionViewModel = new SubscriptionViewModel(); //HTTP REQUEST SET UP var httpRequest = new Mock<HttpRequestBase>(); httpRequest.Setup(r => r.Path).Returns("/Subscription/SendEmail"); httpRequest.Setup(r => r.Form).Returns(delegate() { var nv = new NameValueCollection(); nv.Add("FirstName", "John"); nv.Add("LastName", "Smith"); nv.Add("Email", " jsmith@host.com "); nv.Add("Comments", "Comments are here..."); nv.Add("ReceiveUpdates", "true"); return nv; }); //HTTP CONTEXT SET UP var httpContext = new Mock<HttpContextBase>(); httpContext.Setup(c => c.Request).Returns(httpRequest.Object); var subscriptionController = new Mock<SubscriptionController>(); subscriptionController.Setup(s => s.HttpContext).Returns(httpContext.Object); var result = subscriptionController.Object.SendEmail(subscriptionViewModel); Assert.AreEqual(((ViewResult)result).ViewName, "Index"); } } 

I have a controller called SubscriptionController. There is an action method called SendEmail. I want to be able to run my / SendEmail subscription using this testing method above. My view is a form with the following fields: First name, Last name, Email, Comments and checkbox. I need to make fun of this form, as well as make fun of my controller, http request and context. I'm a little confused about what to taunt and what to use as real. Thanks for any clarification.

+6
source share
1 answer

Answer: you can make fun of the form message by directly setting the form values ​​in Request.Form. Before I made fun of HttpRequest, set the values ​​of the form, and then associated the httpRequest object with the HttpContext object. This approach did not work.

I post the solution below, just compare what I did before and you will understand.

 [TestMethod] public void TestSendSubscriptionEmail() { //HTTP CONTEXT SET UP var httpContext = new Mock<HttpContextBase>(); var routeData = new RouteData(); httpContext.Setup(c => c.Request.RequestContext.RouteData).Returns(routeData); httpContext.Setup(c => c.Request.Form).Returns(delegate() { var nv = new NameValueCollection(); nv.Add("FirstName", "John"); nv.Add("LastName", "Smith"); nv.Add("Email", " jsmith@host.com "); nv.Add("Comments", "Comments are here..."); nv.Add("ReceiveUpdates", "true"); return nv; }); httpContext.Setup(c => c.Request.Path).Returns("/Subscription/SendEmail"); var subscriptionViewModel = new Mock<ISubscriptionViewModel>(); subscriptionViewModel.Setup(h => h.HttpContext).Returns(httpContext.Object); subscriptionViewModel.Setup(h => h.FirstName).Returns(httpContext.Object.Request.Form["FirstName"]); subscriptionViewModel.Setup(h => h.LastName).Returns(httpContext.Object.Request.Form["LastName"]); subscriptionViewModel.Setup(h => h.Email).Returns(httpContext.Object.Request.Form["Email"]); subscriptionViewModel.Setup(h => h.Comments).Returns(httpContext.Object.Request.Form["Comments"]); subscriptionViewModel.Setup(h => h.InvestmentUpdates).Returns(bool.Parse(httpContext.Object.Request.Form["ReceiveUpdates"])); var subscriptionController = new SubscriptionController(subscriptionViewModel.Object); var result = subscriptionController.SendEmail(subscriptionViewModel.Object); Assert.AreEqual(((ViewResult)result).ViewName, "Index"); } 
+6
source

Source: https://habr.com/ru/post/903773/


All Articles