How to mock the Request.form for only a few values on Controller in ASP.Net MVC?

1.1k Views Asked by At

I am new to Nunit and Moq framework and am totally stuck in testing for one of my controller as it fetches some of the values from Request.Form["Something"]. I am able to test for the other part of the controller method using nunit but the code is breaking wherever Request.form values are fetched in the same controller.

Below is the test controller code using nunit and Moq:

      [Test]
    public void SaveRequest()
    {
        TestController test = new TestController();

        TestModel model = new TestModel();
        model = PopulateRequestModel();
        Mail = model.Mail;
        HttpContext.Current = new HttpContext(new HttpRequest("", "https://localhost", ""), new HttpResponse(new System.IO.StringWriter()));
        System.Web.SessionState.SessionStateUtility.AddHttpSessionStateToContext(HttpContext.Current, new HttpSessionStateContainer("", new SessionStateItemCollection(), new HttpStaticObjectsCollection(), 20000, true, HttpCookieMode.UseCookies, SessionStateMode.InProc, false));

        System.Web.HttpContext.Current.Session["__RequestVerificationTokenError"] = false;

        var httpContext1 = (new Mock<HttpContextBase>());
        var routeData = new RouteData();
        httpContext1.Setup(c => c.Request.RequestContext.RouteData).Returns(routeData);

        httpContext1.Setup(c => c.Request.Form).Returns(delegate()
        {
            var nv = new NameValueCollection();
            nv.Add("Firstname", "Dave");
            nv.Add("LastName", "Smith");
            nv.Add("Email", "[email protected]");
            nv.Add("Comments", "Comments are here...");
            nv.Add("ReceiveUpdates", "true");
            return nv;
        });


        if (LoggedInEnterpriseId != null)
        {
            ViewResult result = test.SaveRequest(model, mail) as ViewResult;
            Assert.IsNotNull(result);
        }
    }

PopulateRequestModel :

        private RequestModel PopulateRequestModel ()
    {
        RequestModel model = new RequestModel ();

        model.Firstname = "Dave";
        model.LastName = "Smith";
        model.Email = "[email protected]";
        model.Comments = "Comments are here...";
        model.ReceiveUpdates = true;


        return model;

    }

Below is the actual controller method to be tested :

     [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult SaveRequest(TestModel model, HttpPostedFileBase Mail)
    {

        string Name = Request.Form["Firstname"].ToString();
        model.Mail = Mail;

        //Start Adding Request to DB
        Logger.Write("Start: Adding new request..");
        try
        {
            using (db = new DatabaseContext())
            {
                NewRequest newRequest = new NewRequest();

                if (RequestNumber != 0)
                {
                    newRequest.Name = Convert.ToInt16(Request.Form["Firstname"]);
                    newRequest.LastName = Request.Form["LastName"];
                    newRequest.Email = Request.Form["Email"];
                    newRequest.Comments = Request.Form["Comments"];                       
                    newRequest.ReceiveUpdates = Convert.ToBoolean(Request.Form["ReceiveUpdates"]);               
                }
                else
                {

                    newRequest.Name = model.Firstname;
                    newRequest.LastName = model.LastName;
                    newRequest.Email = model.Email;
                    newRequest.Comments = model.Comments;                       
                    newRequest.ReceiveUpdates = model.ReceiveUpdates;     

                }
                db.NewRequests.Add(newRequest);
                int save = db.SaveChanges();

    }   

The code is getting blocked wherever request.form is used.

0

There are 0 best solutions below