gpt4 book ai didi

c# - 使用 Moq 模拟使用可选参数的方法

转载 作者:太空狗 更新时间:2023-10-30 01:32:23 28 4
gpt4 key购买 nike

我有一个具有以下接口(interface)的消息框服务

public interface IMessageBoxService
{
DialogResult DisplayMessage(IWin32Window owner, string text,
string caption, MessageBoxButtons buttons, MessageBoxIcon icon,
MessageBoxDefaultButton defaultButton = MessageBoxDefaultButton.Button1);
}

它基本上包装了 System.Windows.Forms 消息框,并允许我模拟显示消息框的代码部分。现在我有一个文本文档的搜索服务,如果搜索循环,它会显示“没有更多的位置”消息。我想为这个类的功能写一个单元测试,FindNextMethod

public TextRange FindNext(IDocumentManager documentManager, IMessageBoxService messageBoxService, 
TextEditorControl textEditor, SearchOptions options, FindAllResultSet findAllResults = null)
{
...
if (options.SearchType == SearchType.CurrentDocument)
{
Helpers.SelectResult(textEditor, range);
if (persistLastSearchLooped)
{
string message = MessageStrings.TextEditor_NoMoreOccurrances;
messageBoxService.DisplayMessage(textEditor.Parent, message,
Constants.Trademark, MessageBoxButtons.OK, MessageBoxIcon.Information); <- Throws here.
Log.Trace($"TextEditorSearchProvider.FindNext(): {message}");
lastSearchLooped = false;
}
}
...
}

我的测试是

[TestMethod]
public void FindInCurrentForwards()
{
// Mock the IMessageBoxService.
int dialogShownCounter = 0;
var mock = new Mock<IMessageBoxService>();
mock.Setup(m => m.DisplayMessage(It.IsAny<IWin32Window>(), It.IsAny<string>(), It.IsAny<string>(),
It.IsAny<MessageBoxButtons>(), It.IsAny<MessageBoxIcon>(), It.IsAny<MessageBoxDefaultButton>()))
.Returns(DialogResult.OK)
.Callback<DialogResult>(r =>
{
Trace.WriteLine($"MockMessageBoxService {r.ToString()}");
dialogShownCounter++;
});

// Start the forward search through the first document.
var options = new SearchOptions()
{
SearchText = "SomeText",
SearchType = SearchType.CurrentDocument,
MatchCase = false,
MatchWholeWord = false,
SearchForwards = false
};
var searchProvider = new TextEditorSearchProvider();
var textEditor = ((TextEditorView)documentManager.GetActiveDocument().View).TextEditor;

TextRange range = null;
for (int i = 0; i < occurances + 1; ++i)
range = searchProvider.FindNext(documentManager, mock.Object, textEditor, options);

// We expect the text to be found and the dialog to be displayed once.
Assert.IsNotNull(range);
Assert.AreEqual(1, dialogShownCounter);
}

但是我得到了一个

System.Reflection.TargetParameterCountException Parameter count mismatch.

我看过这个question我似乎按照答案的建议做了,并提供了可选参数,但我仍然遇到异常,为什么?

我看到了一个 answer here建议我必须使用具有正确参数计数的 .Result,所以我尝试了

mock.Setup(m => m.DisplayMessage(It.IsAny<IWin32Window>(), It.IsAny<string>(), It.IsAny<string>(), 
It.IsAny<MessageBoxButtons>(), It.IsAny<MessageBoxIcon>(), It.IsAny<MessageBoxDefaultButton>()))
.Returns((IWin32Window owner, string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon,
MessageBoxDefaultButton defaultButton) => DialogResult.OK)
.Callback<DialogResult>(r =>
{
Trace.WriteLine($"MockMessageBoxService {r.ToString()}");
dialogShownCounter++;
});

感谢您的宝贵时间。

最佳答案

抛出TargetParameterCountException是因为你的回调注册只注册了一个参数。

.Callback<DialogResult>(r =>
{
Trace.WriteLine($"MockMessageBoxService {r.ToString()}");
dialogShownCounter++;
});

Callback 不能接受 Returns 返回的值。它仍然必须匹配模拟的方法签名。

.Callback((IWin32Window a1, string a2,
string a3, MessageBoxButtons a4, MessageBoxIcon a5,
MessageBoxDefaultButton a6) => { dialogShownCounter++ });

关于c# - 使用 Moq 模拟使用可选参数的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37412572/

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