Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hello,

I am working on Unit testing the MVC3 site using NUnit, MvcContrib.TestHelper package but I am facing the exception "Object reference not set to an instance of an object" when my test method accesses the controller having TryUpdateModel. I dont know what to do to pass the test. please help me in this.

I am also giving a code for that :

Action from Controller which test method calls, is given below :

public JsonResult AddPatient()
       {

           bool returnStatus;
           string returnErrorMessage;

           List<string> returnMessage;

           PatientBLL patientBLL = new PatientBLL();

           Models.PatientViewModel patientViewModel = new Models.PatientViewModel();

           TryUpdateModel(patientViewModel);

           Patient patient = patientBLL.AddPatient(
               patientViewModel,
               out returnMessage,
               out returnStatus,
               out returnErrorMessage);

           patientViewModel.UpdateViewModel(patient, typeof(Patient).GetProperties());

           patientViewModel.ReturnMessage = returnMessage;
           patientViewModel.ReturnStatus = returnStatus;

           return Json(patientViewModel);

       }




and the test method which calls the above action is given below :

[Test]
       public void Test_AddPatient()
       {
           TestControllerBuilder builder = new TestControllerBuilder();

           string uniquePatientKey = GenerateUniqueID();

           builder.Form["MedicalID"] = uniquePatientKey;
           builder.Form["SocialSecurityNumber"] = uniquePatientKey;
           builder.Form["FirstName"] = "Khushi";
           builder.Form["LastName"] = "Maahi";
           builder.Form["AddressLine1"] = "ABCD";
           builder.Form["AddressLine2"] = "Technologies";
           builder.Form["City"] = "OOna";
           builder.Form["State"] = "UP";
           builder.Form["ZipCode"] = "98456-7329";
           builder.Form["PhoneNumber"] = "(425)882-8080";
           builder.Form["DateOfBirth"] = "10/28/1987";
           builder.Form["PatientDateOfBirth"] = "10/28/1987";
           builder.Form["EffectiveDate"] = "01/01/1995";
           builder.Form["PatientEffectiveDate"] = "01/01/1995";

           PatientController patientController = builder.CreateController<PatientController>();
          JsonResult jsonResult = (JsonResult)patientController.AddPatient();

           dynamic jsonData = jsonResult.Data;
           string jsonMessage=Convert.ToString(jsonData.ReturnMessage);
           Assert.AreEqual(jsonData.ReturnStatus, true );
           Assert.Greater(jsonData.PatientID, 0);


       }



Please give me the solution for my probelm.
Posted
Comments
Sergey Alexandrovich Kryukov 29-Nov-11 20:01pm    
What kind of help can you expect if you did not provide exception dump, a line of code where exception was thrown is not shown, TryUpdateModel definition is not shown.
--SA

1 solution

It sounds like you goal is to pass the test. You goal is just the opposite: to fail the test and figure out what's wrong in the code, get understanding of it. :-)

Now, please see my comment to the question. Is it clear? Don't bother to supply missing information. Instead, just run the test under debugger. This is such a kind of exception that anyone who can develop software in principle will immediately see the problem when the code is run under debugger. Remember: you need to fix functionality, not the test.

—SA
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900