gpt4 book ai didi

C# - 验证模拟(MoQ)属性的方法是用部分字符串作为参数调用的

转载 作者:行者123 更新时间:2023-12-03 18:47:12 24 4
gpt4 key购买 nike

我正在使用 MoQ 和 C# 来模拟公共(public)属性,我想知道是否使用以特定字符集开头的任何字符串调用了模拟方法之一。

例如,虽然我知道这可行:

mockLogger.Verify(x => x.Information($"Entering {methodName}"), Times.Once);

我正在尝试通过以下尝试查看 mockLoggerInformation()使用以 $"Exception in {methodName} - Error Message: {ex.Message} - StackTrace:" 开头的参数调用方法
mockLogger.Verify(x => x.Information($"Exception in {methodName}: " +
$"Error Message: {exceptionMessage} - " +
$"StackTrace: ........"), Times.Once);

这不可能吗?还是有某种解决方法?

编辑:

我什至试过
    mockLogger.Verify(x => x.Information($"Exception in {methodName}: " +
$"Error Message: {exceptionMessage} - " +
$"StackTrace: " + It.IsAny<string>()),
Times.Once);

但它似乎也不起作用。

最佳答案

您也可以使用 It.Is<string>()可以进行比较。

string searchString = $"Exception in {methodName}: " +
$"Error Message: {exceptionMessage} - " +
$"StackTrace: ";
mockLogger.Verify(x => x.Information(It.Is<string>(s => s.StartsWith(searchString))), Times.Once);

这可能比使用 It.IsRegex() 更清晰。正如我之前建议的那样。

关于C# - 验证模拟(MoQ)属性的方法是用部分字符串作为参数调用的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39240853/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com