我在我的代碼中使用的測試,並通過我的理解,所有的測試要求的方法運行是這樣的:爲什麼我的Assert失敗? (拋出異常)
private ClassName(Argument)
{
return;
}
那麼究竟是什麼測試實際上是檢查是否「參數「是方法/測試結束時的正確輸出。
但在我的情況下,傳入的參數與輸出無關。因此我不確定如何測試它。
注意:代碼本身是健全的,它會輸出正確的變量。此外,我實際上寫了代碼後測試(大booboo,我知道,我想養成總是測試的習慣。)
編輯:我相信問題是關於測試正在退出通過LoginView.Close
和LoginView.Menu.SetMenuView
。 (該功能在此行結束,測試無法完成?)
return;
的位置也會影響其餘代碼,因此它需要位於代碼末尾。如果我移動它,其餘的代碼變得無法訪問,並且程序中斷。
我一直在尋找方法來繞過異常,而我所想到的就是測試異常實際上是否被正確拋出。 有沒有辦法繞過測試中的異常?
下面是測試:
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);
}
這裏正在測試方法:
public static void Compare_Login(User_LoginView LoginView)
{
// Creates a new oject 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 = AccessModel.AccessLevel;
Menu_View.accessLevelSet = AccessModel.AccessLevel;
// 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);
// Exception Thrown in testing.
LoginView.Close();
LoginView.Menu.SetMenuView();
}
else
{
ErrorCodes_Controller LoginError = new ErrorCodes_Controller();
LoginError.WrongLoginError();
}
return;
}
你應該把它分解成更小的函數來完成離散任務,驗證密碼)&測試那些。 – SLaks
有沒有聽說過正則表達式? – Matthias
那到底發生了什麼?你期望人們在瀏覽器中調試你的代碼嗎?我看到stackoverflow正在變成'makemyhomeworkforme.com'。 –