2
public interface IMouseTraits
{
    void ForceCursor(Cursor cursor);
    void SetCursor(Cursor cursor);
}

1)我如何验证对 SetCursor 的最后一次调用是Cursors.Arrow(多次调用 SetCursor 和 ForceCursor 并允许使用不同的参数)。

2)我如何验证两者的最后一次调用SetCursorForceCursorSetCursor多次调用 SetCursor 和 ForceCursor 并允许使用不同的参数)。

[Test]
public void AfterOperationCursorIsArrow()
{
    var mock = new Mock<IMouseTraits>();

    varw = new WindowOperation(mock.Object);
    w.Execute();

    mock.Verify(traits => traits.SetCursor(Cursors.Arrow));  // ?
}
4

1 回答 1

5

您可以使用回调来捕获调用:

[Test]
public void AfterOperationCursorIsArrow()
{
    string lastMethod = null;
    Cursor lastCursor = null;

    var mock = new Mock<IMouseTraits>();

    mock.Setup(m => m.ForceCursor(It.IsAny<Cursor>()))
        .Callback((Cursor c) => lastMethod = "ForceCursor");

    mock.Setup(m => m.SetCursor(It.IsAny<Cursor>()))
        .Callback((Cursor c) => { 
            lastMethod = "SetCursor";
            lastCursor = c; 
        });

    var w = new WindowOperation(mock.Object);
    w.Execute();

     Assert.That(lastMethod, Is.EqualTo("SetCursor"));
     Assert.That(lastCursor, Is.EqualTo(Cursors.Arrow));
}
于 2013-01-25T10:58:50.283 回答