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.
source share