C# 测试断言失败:空引用异常。对象引用未设置为对象的实例

C# 测试断言失败:空引用异常。对象引用未设置为对象的实例,c#,asp.net-mvc,nullreferenceexception,object-reference,objectinstantiation,C#,Asp.net Mvc,Nullreferenceexception,Object Reference,Objectinstantiation,好吧,我有一行代码让我很难过,但不幸的是,没有它,我的程序会崩溃。这是我唯一能想到的方法(除了完全重组我的程序)去做我想让它做的事情 此行的目的是选择要运行的下一个类(在菜单中),该类不在它自己的范围内。问题是类(登录)存在于菜单范围内 这是一句话: LoginView.Menu.SetMenuView(); 有人能提出更好的写作方法吗? 从技术上讲,线路没有问题,程序运行顺利。但是当我运行测试时,由于Null引用异常,测试失败 以下是它出现在中的类: 控制器: public void Sho

好吧,我有一行代码让我很难过,但不幸的是,没有它,我的程序会崩溃。这是我唯一能想到的方法(除了完全重组我的程序)去做我想让它做的事情

此行的目的是选择要运行的下一个类(在菜单中),该类不在它自己的范围内。问题是类(登录)存在于菜单范围内

这是一句话:

LoginView.Menu.SetMenuView();
有人能提出更好的写作方法吗?

从技术上讲,线路没有问题,程序运行顺利。但是当我运行测试时,由于Null引用异常,测试失败

以下是它出现在中的类:

控制器:

public void Show_Login(Menu_View Main_Menu)
    {
        // Creates an object of the User_LoginView.
        // Set the Parent Form of the Child window
        // Display the new form.
        User_LoginView LoginView = new User_LoginView(); 
        LoginView.MdiParent = Main_Menu;

        // This line changes the reference to the "Menu" variable for later use. 
        LoginView.Menu = Main_Menu;
        LoginView.Show(); 
    }

public static void Compare_Login(User_LoginView LoginView)
    {
        // Creates a new object of User_Model and populates it from the User_Controller.
        User_Model AccessModel = new User_Model();
        AccessModel.Name = screenName;
        AccessModel.Pwd = screenPwd;

        // Runs the Login Comparsion in the Database_Facade, and passes in the Model.
        Database_Facade Database = new Database_Facade();
        Database.GetLoginAccess(AccessModel);
        // screenAccess used for testing.
        screenAccess = AccessModel.AccessLevel;
        Menu_View.accessLevelSet = AccessModel.AccessLevel;

        // If the return is placed here, the assert passes, but the rest of the code 
        // becomes unreachable.
        // return;

        // Compares the returned AccessLevel. 
        // if it is corect; closes the Login and runs the SetMenuView method,
        // if it is incorrect; shows an error.
        if (AccessModel.AccessLevel > 0)
        {
            Console.WriteLine("Access Level " + AccessModel.AccessLevel);
            LoginView.Menu.SetMenuView();
            LoginView.Close();
            Menu_View.accessLevelSet = AccessModel.AccessLevel;
        }
        else
        {
            ErrorCodes_Controller LoginError = new ErrorCodes_Controller();
            LoginError.WrongLoginError();
        } 
    }
视图:

以下是测试:

    [TestMethod()]
    public void Compare_LoginTest()
    {
        User_LoginView LoginView = new User_LoginView();

        User_Controller.screenName = "ben";
        User_Controller.screenPwd = "password";
        User_Controller.Compare_Login(LoginView);
        int actual = User_Controller.screenAccess;
        int expected = 1;
        Assert.AreEqual(expected, actual);
    }

在测试方法中,您没有为
LoginView.Menu
设置值,而
正在
Show\u Login()
中执行该操作

因此,当您从测试调用
Compare\u Login()
时,
菜单
为空


更改
LoginView
的构造函数以接受菜单并确保测试通过可能是值得的。

您应该重构可测试代码,使其不依赖于UI。对不起,这是什么意思?几乎所有
NullReferenceException
的情况都是一样的。有关一些提示,请参见“”。在测试中简单添加了
LoginView.Menu
,解决了此问题。谢谢你的提示!
    [TestMethod()]
    public void Compare_LoginTest()
    {
        User_LoginView LoginView = new User_LoginView();

        User_Controller.screenName = "ben";
        User_Controller.screenPwd = "password";
        User_Controller.Compare_Login(LoginView);
        int actual = User_Controller.screenAccess;
        int expected = 1;
        Assert.AreEqual(expected, actual);
    }