[英]I cant figure out whats wrong with my MVC unit test code
okay guys i am new with MVC and i am trying to learn from tutorial i did exactly as said but it throws me 好的,伙计们,我是MVC的新手,我正尝试从教程中学习,我完全按照说的做,但这使我失望
Test method TestProject1.UnitTest1.DisplayCustomer threw exception: System.NullReferenceException: Object reference not set to an instance of an object. 测试方法TestProject1.UnitTest1.DisplayCustomer引发异常:System.NullReferenceException:对象引用未设置为对象的实例。 when i try to run unit test 当我尝试运行单元测试时
I am following tutorial from here http://www.codeproject.com/Articles/259560/Learn-MVC-Model-view-controller-Step-by-Step-in-7 我正在从这里关注教程http://www.codeproject.com/Articles/259560/Learn-MVC-Model-view-controller-Step-by-Step-in-7
Here are my Files : 这是我的文件:
DisplayCustomer View DisplayCustomer视图
Customer ID is : <%= Model.Id %>
Customer ID is : <%= Model.CustomerCode %>
<% if (Model.Amount >100) {%>
This is a Previlaged Customer
<%} else { %>
This is a Normal Customer
<% } %>
Fill Customer View: 填写客户视图:
<form action="DisplayCustomer" method = "post">
Customer ID : <input type="text" name="CustomerId" /></br>
Customer Code : <input type="text" name="CustomerCode" /></br>
Customer Amount : <input type="text" name="CustomerAmount" /></br>
<input type="submit" value="Click Here"/></br>
</form>
Customer Model: 客户模型:
public class Customer
{
public int Id { set; get; }
public string CustomerCode { set; get; }
public double Amount { set; get; }
}
CustomerController CustomerController
public class CustomerController : Controller
{
//
// GET: /Customer/
public ActionResult Index()
{
return View();
}
public ActionResult FillCustomer()
{
return View();
}
public ActionResult DisplayCustomer()
{
Customer objCustomer = new Customer();
objCustomer.Id = 10;
objCustomer.CustomerCode = "Sparkz";
objCustomer.Amount = 10.55;
var myview = View(objCustomer);
return myview;
}
}
My unit test File(UnitTest1.cs): Here i tried both the codees the commented one and the one i wrote below but neither work. 我的单元测试File(UnitTest1.cs):在这里,我尝试了注释的代码和我在下面编写的代码,但均无效。 It always fetches null in the var customerViewViewResult /varresult 它总是在var customerViewViewResult / varresult中获取null。
[TestClass]
public class UnitTest1
{
[TestMethod]
public void DisplayCustomer()
{
//CustomerController obj = new CustomerController();
//var varresult = obj.DisplayCustomer() as ViewResult;
//Assert.AreEqual("DisplayCustomer", varresult.ViewName);
CustomerController controller = new CustomerController();
var customer = new Customer();
var customerViewActionResult = controller.DisplayCustomer();
var customerViewViewResult = customerViewActionResult as ViewResult;
Assert.AreEqual("DisplayCustomer", customerViewViewResult.ViewName);
}
}
I am following tutorial from here 我正在从这里关注教程
In the tutorial, the return type of DisplayCustomer
is ViewResult
, not ActionResult
在本教程中, DisplayCustomer
的返回类型是ViewResult
,而不是ActionResult
// As stated in the tutorial
public ViewResult DisplayCustomer()
{
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.