gpt4 book ai didi

c# - 模拟 protected 方法总是返回 true

转载 作者:行者123 更新时间:2023-12-03 10:58:49 24 4
gpt4 key购买 nike

我的基类中有以下 protected 方法:

protected bool ExecuteInteraction(string identifier,
Action<object> interactionFinished, params object[] args)
{
Contract.Requires(!string.IsNullOrEmpty(identifier));
Contract.Requires(interactionFinished != null);

bool result = false;

if (!string.IsNullOrEmpty(identifier) && (interactionFinished != null))
{
// Find the matching interaction behavior.
IInteractionBehavior interactionBehavior =
InteractionBehaviorCollection.Instance.Get(identifier);

if (interactionBehavior != null)
{
try
{
interactionBehavior.Execute(interactionFinished, args);
result = true;
}
catch (Exception)
{
}
}
else
{
Debug.WriteLine(string.Format(
"InteractionBehavior with identifier [{0}] is unknown",
identifier));
}
}

return result;
}

基本上来自这篇文章: http://www.codeproject.com/Articles/708346/Dialogs-the-MVVM-Way ...

此方法在我的派生类中使用,如下所示:
protected override void Delete()
{
try
{
if (PrimaryModel.Id != 0 && PrimaryModel.AddressId != 0)
{
ExecuteInteraction("DeleteConfirmation", (result) =>
{
MessageBoxResult messageBoxResult = (MessageBoxResult)result;

if (messageBoxResult == MessageBoxResult.Yes)
{
//Do something ...
}
});
}
}
catch (Exception exc)
{
Message = exc.Message;
}
}

这在我的单元测试中不起作用,因为我没有模拟 messageBoxResult 是 MessageBoxResult.Yes。这就是为什么我希望我的 protected 方法在单元测试中始终返回 true 的原因。我该怎么做(起订量)?

最佳答案

您只需连接您的MessageBox操作,以便您可以提供 MockMessageBox当单元测试总是返回 MessageBoxResult.Yes .因此,在您的代码中,不要引用任何 MessageBox直接es。创建 WindowManagerMessageBoxService您调用的类,有点像代理消息框。

例如,尝试这样的事情:

public string ShowMessageBox(string message, string title, string buttons, string icon)
{
MessageBoxButton messageBoxButtons;
switch (buttons.ToLower())
{
case "ok": messageBoxButtons = MessageBoxButton.OK; break;
case "okcancel": messageBoxButtons = MessageBoxButton.OKCancel; break;
case "yesno": messageBoxButtons = MessageBoxButton.YesNo; break;
case "yesnocancel": messageBoxButtons = MessageBoxButton.YesNoCancel; break;
default: messageBoxButtons = MessageBoxButton.OKCancel; break;
}
MessageBoxImage messageBoxImage;
switch (icon.ToLower())
{
case "asterisk": messageBoxImage = MessageBoxImage.Asterisk; break;
case "error": messageBoxImage = MessageBoxImage.Error; break;
case "exclamation": messageBoxImage = MessageBoxImage.Exclamation; break;
case "hand": messageBoxImage = MessageBoxImage.Hand; break;
case "information": messageBoxImage = MessageBoxImage.Information; break;
case "none": messageBoxImage = MessageBoxImage.None; break;
case "question": messageBoxImage = MessageBoxImage.Question; break;
case "stop": messageBoxImage = MessageBoxImage.Stop; break;
case "warning": messageBoxImage = MessageBoxImage.Warning; break;
default: messageBoxImage = MessageBoxImage.Stop; break;
}
return MessageBox.Show(message, title, messageBoxButtons, messageBoxImage).ToString();
}

这将被称为:
string result = WindowManager.ShowMessageBox(message, title, "OkCancel", "Question");

请注意与任何 UI dll 没有直接关系......所有属性都是基于字符串的。现在你需要接口(interface)这个方法所在的类( WindowManager)。所以现在我们有一个 IWindowManager这迫使我们写一个 ShowMessageBox所有实现类中的方法。

现在我们在 MockWindowManager 中实现这个接口(interface)类(class):
public string ShowMessageBox(string message, string title, string buttons, string icon)
{
switch (buttons)
{
case "Ok":
case "OkCancel": return "OK";
case "YesNo":
case "YesNoCancel": return "Yes";
default: return "OK";
}
}

运行应用程序时,我们使用 WindowManager类和测试时,我们使用 MockWindowManager类(class)。这可以通过多种方式实现,但最简单的方法是通过 IWindowManager进入 View 模型构造函数:
public YourViewModel(IWindowManager windowManager)
{
this.windowManager = windowManager;
}

关于c# - 模拟 protected 方法总是返回 true,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27604648/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com