Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我不知道我的MVC单元测试代码有什么问题_C#_Asp.net_Asp.net Mvc - Fatal编程技术网

C# 我不知道我的MVC单元测试代码有什么问题

C# 我不知道我的MVC单元测试代码有什么问题,c#,asp.net,asp.net-mvc,C#,Asp.net,Asp.net Mvc,好了,伙计们,我是MVC的新手,我正试图从教程中学习,我完全按照上面说的做了,但这让我很困惑 测试方法TestProject1.UnitTest1.DisplayCustomer引发异常: System.NullReferenceException:对象引用未设置为对象的实例。 当我尝试运行单元测试时 我从这里开始学习教程 这是我的档案: 显示客户视图 Customer ID is : <%= Model.Id %> Customer ID is : <%= M

好了,伙计们,我是MVC的新手,我正试图从教程中学习,我完全按照上面说的做了,但这让我很困惑

测试方法TestProject1.UnitTest1.DisplayCustomer引发异常: System.NullReferenceException:对象引用未设置为对象的实例。 当我尝试运行单元测试时

我从这里开始学习教程

这是我的档案:

显示客户视图

    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
    <% } %>

客户控制器

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

}
我的单元测试文件(UnitTest1.cs): 在这里,我尝试了注释的代码和我在下面写的代码,但都不起作用。 它总是在变量customerViewResult/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);
    }
}
我从这里开始学习教程

在本教程中,
DisplayCustomer
的返回类型是
ViewResult
,而不是
ActionResult

// As stated in the tutorial
public ViewResult DisplayCustomer()
{
    ...
}

调试器说什么?
[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);
    }
}
// As stated in the tutorial
public ViewResult DisplayCustomer()
{
    ...
}