【发布时间】:2022-08-02 10:50:34
【问题描述】:
我有一个模拟的具体类,当我尝试计算类中调用方法“x”的次数时,我得到一个异常。我知道这不是一个模拟接口,并且该方法不可覆盖。有没有其他方法可以计算?
我在嘲笑 RestSharp 的 \"RestClient\" 类。我实际上可以使用 RestClient 而无需嘲笑它。但我无法说出这个类的“执行”方法被调用了多少次。我需要这个来测试重试机制是否启动并尝试进行 http 调用 \"x\" 次数
Mock<RestClient> _mockRestClient = new Mock<RestClient>(mockHttpHandler, true);
//Act
var res = _httpClient.ExecuteTaskWithPolicy(_mockRestClient.Object, _mockRestRequest.Object, policy);
//Assert
_mockRestClient.Verify(x => x.Execute(_mockRestRequest.Object), Times.Exactly(4));
Non-overridable members (here: RestClient.Execute) may not be used in setup / verification expressions.\'
-
你能分享你在嘲笑什么方法,以及你是如何做到的吗?
-
\"我得到一个异常\"什么异常?通常,尝试模拟不是接口的东西是个坏主意。
-
Exception-\"Non-overridable members (here: RestClient.Execute) may not be used in setup / verification expressions.\'我可以避免嘲笑RestClient并直接使用它,但我无法计算内部方法的次数它被称为
-
如果无法设置方法,则无法使用 Moq 来计算调用了多少次。