C# 设置 moq 并验证方法是否被调用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9040049/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 05:42:46  来源:igfitidea点击:

Setting up moq and verifying that a method was called

c#.nettddmoq

提问by dev.e.loper

Using Microsoft Test Framework and Moq I'm trying to verify if a log4net method was called.

使用 Microsoft 测试框架和 Moq 我试图验证是否调用了 log4net 方法。

    [TestMethod()]
    public void Log_Info_When_Stuff_Is_Done()
    {
        SampleClass sampleObject = new SampleClass(); 

        Mock<log4net.ILog> logMockObject = new Mock<log4net.ILog>();

        sampleObject.Log = logMockObject.Object;

        sampleObject.DoStuffAndLogInfo();

        logMockObject.Verify(moqLog => moqLog.Info("do stuff got called"), Times.AtLeastOnce());

    }

I get an exception on Verify call saying that

我在验证电话上收到一个异常说

Expected invocation on the mock at least once, but was never performed: moqLog => moqLog.Info("do stuff got called") No setups configured. No invocations performed.

预期在模拟上至少调用一次,但从未执行过:moqLog => moqLog.Info("do stuff got called") 未配置设置。未执行任何调用。

What am I doing wrong?

我究竟做错了什么?

updatethe problem was with a getter for SampleClas.Log property. I was always returning LogManager.GetLogger(...);even when the property was already set to a ILogProxy. I was under impression that the property's get accessor won't be called because I've set up a proxy like so sampleObject.Log = logMockObject.Object;

更新问题在于 SampleClas.Log 属性的吸气剂。LogManager.GetLogger(...);即使该属性已设置为 ILogProxy,我也总是会返回。我的印象是不会调用该属性的 get 访问器,因为我已经设置了这样的代理 sampleObject.Log = logMockObject.Object;

回答by Cristian Lupascu

The test is correctly set up.

测试设置正确。

Check your sutto see if Log.Infoactually gets called inside the DoStuffAndLogInfomethod.

检查您的sut以查看是否Log.Info真的在DoStuffAndLogInfo方法内部被调用。

回答by Michael Liu

Right now Moq is verifying that DoStuffAndLogInfocalls Infowith the exact string"do stuff got called". If it's actually calling Infowith a different argument, and you don't care what the actual argument is, use the following instead:

现在起订量验证DoStuffAndLogInfo调用Info实际字符串“做的东西得到了被称为”。如果它实际上Info使用不同的参数进行调用,而您不关心实际参数是什么,请改用以下内容:

logMockObject.Verify(moqLog => moqLog.Info(It.IsAny<string>()), Times.AtLeastOnce()); 

回答by starbeamrainbowlabs

This doesn't look to be the original poster's problem, but in my case I had a very similar error message. It was due to my .Verify()call beforethe actual execution. For example, this is wrong:

这看起来不是原始海报的问题,但在我的情况下,我有一个非常相似的错误消息。这是由于我实际执行之前.Verify()电话。例如,这是错误的

SampleClass sampleObject = new SampleClass(); 
Mock<log4net.ILog> logMockObject = new Mock<log4net.ILog>();
logMockObject.Verify(moqLog => moqLog.Info(It.IsAny<string>()), Times.AtLeastOnce());
sampleObject.Log = logMockObject.Object;

sampleObject.DoStuffAndLogInfo();

....but this is right:

....但这是正确的

SampleClass sampleObject = new SampleClass(); 
Mock<log4net.ILog> logMockObject = new Mock<log4net.ILog>();
sampleObject.Log = logMockObject.Object;

sampleObject.DoStuffAndLogInfo();

logMockObject.Verify(moqLog => moqLog.Info(It.IsAny<string>()), Times.AtLeastOnce());