在模拟对象上使用 Moq 验证间接调用的方法
我的应用程序有一个 ProviderFactory 静态类,它具有传回记录器等静态实例的静态实用方法。然后,我的应用程序的其余部分可以从任何地方获取对记录器的引用,而无需传入记录器(常见的设计实践)。
因此,我的应用程序的另一部分 DbCacheProvider 具有调用记录器的方法,因此它在内部从工厂获取对记录器的引用,然后对其发出调用。
我的问题是,使用 Moq,我想验证记录器上的方法是否由 DbCacheProvider 中的方法调用。当我将模拟记录器作为参数传递到 DbCacheProvider 时,我可以使用依赖项注入来执行此操作,但我没有传递记录器(我不想这样做)。那么,我如何验证 DbCacheProvider 是否正在调用记录器?
My app has a ProviderFactory static class that has static utility methods passing back static instances of things like a logger. The rest of my app then can just grab a/the reference to the logger from anywhere without having to pass in the logger (common design practice).
So, another part of my app, the DbCacheProvider, has methods that make calls to the logger so internally it gets a reference to the logger from the factory and then issues calls to it.
My question is that using Moq, I want to verify methods on the logger are being called by the methods within the DbCacheProvider. I can do this using dependency injection when I pass a mock logger into the DbCacheProvider as a parameter, but I'm not passing the logger in (not do I want to). So, how would I verify the DbCacheProvider is making calls to the logger?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您不想通过构造函数传递记录器,则需要在运行单元测试时更改 ProviderFactory 以返回模拟的记录器。
无论如何,经常建议设置依赖项注入有几个原因:
If you don't want to pass the logger in through the constructor you'd need to change your ProviderFactory while running unit tests to return your mocked logger.
Anyway there are a couple of reasons it's often suggested to set up dependency injection:
没有答案的老问题,我有一个类似的问题并这样解决:
我有以下示例代码,需要验证不仅调用了一个方法,而且还调用了一个特定的值。
这是正在测试的类,其中正在注入接口项:
这显然是我的实际代码的非常简单的视图,但我需要验证 Step1 和 Step2 的行为是否符合预期并将正确的值传递给日志,这这意味着我还需要确保它们以正确的顺序发生。我的测试:
希望这有帮助。
Old question without an answer, I had a similar problem and solved it like this:
I have the following sample code and need to verify that not only was a method called but was called with a specific value.
This is the class being tested, where the interface items are being injected:
This is obviously a very simplistic view of my actual code, but I needed to verify that Step1 and Step2 are behaving as expected and passed the correct values to the Log, this would mean I also needed to ensure they occurred in the right order. My test:
Hope this helps.