单元测试控制器时出现 NullReferenceException

发布于 2024-12-18 08:59:47 字数 2322 浏览 1 评论 0原文

我正在使用 NUnit、MvcContrib.TestHelper 包对 MVC3 站点进行单元测试,但当我的测试方法访问具有 TryUpdateModel 的控制器时,我遇到异常“对象引用未设置为对象的实例”。我不知道该怎么做才能通过考试。请帮助我。

我还给出了一个代码:

下面给出了测试方法调用的控制器的操作:

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);

    }

下面给出了调用上述操作的测试方法:

[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);


    }

请为我的问题提供解决方案。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

似最初 2024-12-25 08:59:47

您在 PatientViewModel 中的某处有一个 null 。您可以在这里发布该类型吗?这可能是 混合模型和视图模型的问题

You have a null somewhere in patientViewModel. Can you post that type here? This could be a problem with mixing models and view models.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文