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

月亮骑士

我有一个具有以下界面的消息框服务

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参数计数不匹配。

我已经看到了这个问题,并且似乎在做答案建议并提供可选参数,但是我仍然遇到异常,为什么?

我在这里看到一个答案,提示我必须使用.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++;
            });

谢谢你的时间。

亚当·B

之所以会抛出TargetParameterCountException,是因为您的回调注册仅使用一个参数进行了注册。

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

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

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

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

来自分类Dev

使用Moq模拟方法的某些部分

来自分类Dev

使用Moq部分模拟类内部方法

来自分类Dev

使用Moq模拟方法的某些部分

来自分类Dev

使用可选参数的方法重载

来自分类Dev

设置模拟类方法以使用Moq执行Func <Task>参数

来自分类Dev

使用Moq,我该如何模拟改变其作为参数的流的方法?

来自分类Dev

使用Moq模拟GetEnumerator

来自分类Dev

如何在Google Mock中使用可选参数模拟方法?

来自分类Dev

如何在Google Mock中使用可选参数模拟方法?

来自分类Dev

使用Moq无法验证具有默认参数的模拟呼叫

来自分类Dev

模拟的通用方法(Moq库),用于验证从未使用任何参数组合调用过的方法

来自分类Dev

IntelliJ GSDL:使用可选参数定义方法

来自分类Dev

使用Moq模拟ControllerBase请求

来自分类Dev

使用Moq模拟Fluent界面

来自分类Dev

使用 moq 模拟对 IQueryable 的调用

来自分类Dev

如何使用 moq 模拟 ActionExecutingContext

来自分类Dev

使用Moq平等设置模拟的所有方法

来自分类Dev

使用Autofixture,Moq和XUnit的类中的部分模拟方法

来自分类Dev

使用Moq模拟具体类方法的返回值

来自分类Dev

如何使用 Moq 框架模拟一个简单的方法?

来自分类Dev

使用Moq更改参数

来自分类Dev

使用可选参数的assertRaises

来自分类Dev

调用方法时使用字典参数作为可选参数

来自分类Dev

使用模拟方法参数模拟后续步骤

来自分类Dev

使用C#/ Moq Framework模拟具有输入参数的异步DbContext函数

来自分类Dev

Moq ReturnAsync使用许多参数

来自分类Dev

使用Moq模拟不安全的界面

来自分类Dev

在C#中使用Moq模拟

Related 相关文章

热门标签

归档