Note that there are some explanatory texts on larger screens.

plurals
  1. POGot NullReferenceException when Unit testing Controller
    primarykey
    data
    text
    <p>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.</p> <p>I am also giving a code for that :</p> <p>Action from Controller which test method calls, is given below :</p> <pre><code>public JsonResult AddPatient() { bool returnStatus; string returnErrorMessage; List&lt;string&gt; 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); } </code></pre> <p>and the test method which calls the above action is given below : </p> <pre><code>[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&lt;PatientController&gt;(); 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); } </code></pre> <p>Please give me the solution for my probelm.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload