簡體   English   中英

模擬使用Moq使用可選參數的方法

[英]Mocking a Method that uses an Optional Parameter using 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++;
            });

謝謝你的時間。

拋出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++ });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM