好的,所以我有一行代码让我很伤心,但不幸的是,没有它,我的程序就会中断。这是我能想到的唯一方法(除了完全重组我的程序)来做我想做的事情。
这一行的目的是选择下一个要运行的类(在菜单中),它超出了它自己的范围。麻烦的是类(登录)存在于菜单范围内。
这是行:
LoginView.Menu.SetMenuView();
谁能提出一个更好的写这个的方法?
从技术上讲,这条线没有任何问题,程序运行顺利。但是当我运行测试时,由于空引用异常,测试失败。
这是它出现的类:
控制器:
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();
}
}
看法:
public partial class User_LoginView : Form
{
// This is where the new reference is set, for when it gets called
// at the end of the Login Comparison.
public Menu_View Menu { get; set; }
public User_LoginView()
{
InitializeComponent();
}
private void btnLogin_Click(object sender, EventArgs e)
{
User_Controller.Check_Login(this);
}
}
这是测试:
[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);
}