简体   繁体   English

我无法弄清楚我的MVC单元测试代码出了什么问题

[英]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.

相关问题 我想知道我的清单有什么问题 - I am trying to figure out whats wrong with my list 我的代码有什么问题? - whats wrong with my code? 这个MVC项目的单元测试代码有什么问题? 我不断收到“不包含定义”和“没有扩展方法错误” - What is wrong with my unit test code for this MVC project? I keep getting 'does not contain definition' and 'no extension method error' 我的AutoResetEvent代码有什么问题? - Whats wrong with my AutoResetEvent code? 无法找出我自己的自定义例外 - Cant figure out my own custom exception 如何使用 C# 中的代码进行单元测试 - How do I make a unit test out of my code in C# 一圈又一圈的碰撞,无法弄清楚我哪里出了问题 - circle on circle collision, cant figure out where i've gone wrong 我的 ConnectionString 有什么问题? 当我在 Postman 中测试我的 API 时出现错误 - Whats wrong with my ConnectionString? When I test my API in Postman I get an error 什么测试足以对MVC中的“创建”控制器方法进行单元测试? - Whats tests would sufficiently Unit Test a “Create” controller method in MVC? 我得到System.ObjectDisposedException,似乎找不到什么错 - I get System.ObjectDisposedException, cant seem to find whats wrong
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM