c# - 如何监视密封库类中的方法?
问题描述
我有一个分析客户端,它在收到事件时调用了一个跟踪方法。
当我去模拟课堂时,我得到了这个:
Type to mock must be an interface or an abstract or non-sealed class.
有没有办法监视和验证传递给密封类方法的参数?
在 JUnit 中,您可以更自由地执行“spyOn(class.method)...”
这如何在 C# 或 Moq 中完成?
代码如下:
private readonly TelemetryClient _telemetry;
public ClassToTest(TelemetryClient telemetry)
{
_telemetry = telemetry;
}
public async Task ProcessMessageAsync(EventMessage message, CancellationToken cancelToken)
{
try
{
var serviceNotificationEvent = JsonConvert.DeserializeObject<ServiceNotificationEvent>(message.Value);
_telemetry.TrackEvent(AnalyticsConstants.EVENT_RECEIVED);
}
catch (Exception e)
{
_logger.LogError(e, "Error processing event message");
}
}
当前单元测试尝试:
[TestMethod]
public void Test_ReceivedEventAnalytic()
{
Mock<TelemetryClient> _mockTelemetry = new Mock<TelemetryClient>();
_serviceNotificationProcessor.ProcessMessagesAsync(mockMessageList);
IDictionary<String, String> matchingMap = new Dictionary<string, string>();
// build out matchingMap
_mockTelemetry.Verify(t => t.TrackEvent("seamEventReceived", It.IsAny<IDictionary<string, string>>(), null), Times.Once());
}
解决方案
推荐阅读
- assembly - 填充动态分配的内存会冻结程序执行
- javascript - 如何多次运行 Bootstrap Alert?
- javascript - 未捕获的语法错误:JSON.parse 中位置 1 的 JSON 中的意外标记和 (
) - php - 为什么我不能在 php 中做评论( /* 像这个 */ )?
- node.js - 将快递端代码重构为更易使用的格式
- c# - DB First Entity Framework Core 中的 Oracle 将 NUMBER(3) 映射到字节导致运行时异常
- rust - 为什么将 mut 添加到传递的迭代器引用可以解决这个问题?
- python - 包含的 URLconf 'appName.urls' 中似乎没有任何模式
- webpack - webpack watch 结束后运行命令
- node.js - 处理大量出站 HTTP 请求