[英]Moq - Verify the last call to a method
public interface IMouseTraits
{
void ForceCursor(Cursor cursor);
void SetCursor(Cursor cursor);
}
1) How do I verify that the last call to SetCursor was with Cursors.Arrow
(multiple calls to SetCursor and ForceCursor with varying arguments allowed). 1)如何验证最后一次调用SetCursor是使用
Cursors.Arrow
(多次调用SetCursor和ForceCursor并允许使用不同的参数)。
2) How do I verify that the last call of both SetCursor
and ForceCursor
was SetCursor
(multiple calls to SetCursor and ForceCursor with varying arguments allowed). 2)如何验证
SetCursor
和ForceCursor
的最后一次调用是SetCursor
(多次调用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)); // ?
}
You can use Callbacks to capture calls: 您可以使用回叫来捕获呼叫:
[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));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.