C# 测试 N-Unit 中的异常
C# Testing Exceptions in N-Unit
我很难弄清楚我的 Assert 语句应该是什么样子来测试下面 "default" 代码块中的异常 and/or 用户消息。有什么想法吗?
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
我很难弄清楚我的 Assert 语句应该是什么样子来测试下面 "default" 代码块中的异常 and/or 用户消息。有什么想法吗?
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