单元测试控制器时出现NullReferenceException [英] Got NullReferenceException when Unit testing Controller

查看:97
本文介绍了单元测试控制器时出现NullReferenceException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

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

我也为此提供了一个代码:

以下是测试方法调用的Controller的 操作:

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

       }





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





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.

推荐答案

听起来您的目标是通过测试.您的目标恰恰相反:要使测试失败并找出代码中的错误,然后对它有所了解. :-)

现在,请参阅我对问题的评论.清楚吗?不要费心提供缺少的信息.相反,只需在调试器下运行测试.这种例外情况使任何原则上可以开发软件的人都可以在调试器中运行代码时立即看到问题. 请记住:您需要修复功能,而不是测试.

—SA
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


这篇关于单元测试控制器时出现NullReferenceException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆