[英]C# Testing Exceptions in N-Unit
我很難弄清楚我的Assert語句在下面的“默認”代碼塊中應該如何測試異常和/或用戶消息。 有任何想法嗎?
public void LoadLanguage(string language)
{
switch (language)
{
case "Spanish":
ShouldPrintSpanish = true;
break;
case "English":
ShouldPrintEnglish = true;
break;
case "Both":
ShouldPrintSpanish = true;
ShouldPrintEnglish = true;
break;
default:
string ex = "AgreementDetailViewModel.LoadFormsForSelectedLanguage() failed to execute correctly";
ExceptionLog.LogException(new Exception(ex),Environment.MachineName, null, "");
ShowError("User should have been able to select a language from the dropdown");
throw new Exception(ex);
}
}
異常記錄代碼
public static ExceptionLog LogException(Exception exception)
{
return new ExceptionLog(exception, "", "", "", "");
}
private ExceptionLog (Exception exception, string environmentVariables, string computerName, string ipAddress, string createdBy)
{
if (exception == null)
throw new ArgumentException("The exception cannot be null.");
_Exception = exception;
CreateAndSaveParentException(computerName, environmentVariables, ipAddress, createdBy);
CreateAndSaveChildExceptions();
}
使用Assert.Throws<ExceptionType>
。 https://github.com/nunit/docs/wiki/Assert.Throws
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.