我正在通过moq创建一个界面的模拟。
我的应用程序代码使用不同的输入组合调用我的mock的不同方法。当我使用错误的输入来验证()时,就会抛出一个异常,列出所有的方法调用。
我希望获得这些方法调用,执行一些清理,并以不同的格式显示给用户。是否有可能在调用verify之前获取所有方法调用?
示例代码:
var mock = new Mock<ILoveThisFramework>();
mock.Setup(framework => framework.DownloadExists("2.0.0.0"))
.Returns(true);
// Hand mock.Object as a collaborator and exercise it,
// like calling methods on it...
ILoveThisFramework lovable = mock.Object;
bool download = lovable.DownloadExists("2.0.0.0");
// Verify that the given method was indeed called with the expected value at most once
// it will throw exception which will include method invocations. I want to get method invocations out and reformat them.
mock.Verify(framework => framework.DownloadExists("3.0.0.0"), Times.AtMostOnce());
发布于 2017-12-10 19:07:25
好的,如果你想reformat
他们,做一些类似这样的事情
var tags = new List<string>();
var mock = new Mock<ILoveThisFramework>();
mock.Setup(framework => framework.DownloadExists(It.IsAny<string>()))
.Returns((string tag) => {
tags.Add(tag);
return true;
});
Assert.IsTrue(tags.Contains("3.0.0.0"), $"3.0.0.0 never passed to `DownloadExists(), only values passed {string.Join(',', tags)}");
https://stackoverflow.com/questions/47730682
复制相似问题