我对单元测试用例编写完全陌生。我正在将 MVVMLigh 与 WPF 一起使用。是否有必要使用一些第三方测试框架或.Net 单元测试框架就够了?另外如何处理单元测试用例中的静态类?在这种情况下 AppMessages 类。
有人可以指导我如何为以下代码编写单元案例:
public MyViewModel(Participant participant)
{
if (participant != null)
{
this.ParentModel = parentModel;
OkCommand = new RelayCommand(() => OkCommandExecute());
CalculateAmountCommand = new RelayCommand(() => CalculateAmount());
}
else
{
ExceptionLogger.Instance.LogException(Constants.ErrorMessages.FinancialLineCanNotBeNull, "FinancialLineViewModel");
AppMessages.DisplayDialogMessage.Send(Constants.ErrorMessages.FinancialLineCanNotBeNull, MessageBoxButton.OK, Constants.DefaultCaption, null);
}
}
public static class AppMessages
{
enum AppMessageTypes
{
FinancialLineViewDisplay,
FinancialLineViewClose,
DisplayDialogMessage
}
public static class DisplayDialogMessage
{
public static void Send(string message, MessageBoxButton button, string caption, System.Action<MessageBoxResult> action)
{
DialogMessage dialogMessage = new DialogMessage(message, action)
{
Button = button,
Caption = caption
};
Messenger.Default.Send(dialogMessage, AppMessageTypes.DisplayDialogMessage);
}
public static void Register(object recipient, System.Action<DialogMessage> action)
{
Messenger.Default.Register<DialogMessage>(recipient, AppMessageTypes.DisplayDialogMessage, action);
}
}
}
public class ExceptionLogger
{
private static ExceptionLogger _logger;
private static object _syncRoot = new object();
public static ExceptionLogger Instance
{
get
{
if (_logger == null)
{
lock (_syncRoot)
{
if (_logger == null)
{
_logger = new ExceptionLogger();
}
}
}
return _logger;
}
}
public void LogException(Exception exception, string additionalDetails)
{
LogException(exception.Message, additionalDetails);
}
public void LogException(string exceptionMessage, string additionalDetails)
{
MessageBox.Show(exceptionMessage);
}
}