如何使用Moq调用类中的另一种方法 [英] How to Verify another method in the class was called using Moq

查看:81
本文介绍了如何使用Moq调用类中的另一种方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎很简单,但我似乎无法使其正常工作.

This seems like something simple but I can't seem to get it to work.

我有一个带有Save方法的类,该方法仅调用另一个方法ShouldBeCalled().我想验证一下,如果我调用Save(),则另一个方法ShouldBeCalled()至少要执行一次.我以为我可以做以下事情.

I have a class with a Save method that simply calls another method ShouldBeCalled(). I want to verify that if I call Save() that the other method ShouldBeCalled() is executed at least once. I thought that I could do the following.

public class ClassA
{
    public virtual void Save()
    {
        ShouldBeCalled();
    }

    public virtual void ShouldBeCalled()
    {
        //This should get executed
    }
}

[TestFixture]
public class ClassA_Test
{
    [Test]
    public void Save_Should_Call_ShouldBeCalled()
    {
        var mockClassA = new Mock<ClassA>();
        mockClassA.Object.Save();

        mockClassA.Verify(x => x.ShouldBeCalled(), Times.AtLeastOnce());
    }
}

但是我得到了异常对模拟至少预期一次调用,但是从未执行过:x => x.ShouldBeCalled()"

But I get the exception "Expected invocation on the mock at least once, but was never performed: x => x.ShouldBeCalled()"

这只是一个猜测,但是Moq是否用自己的版本覆盖了Save()方法,而该版本却忽略了我在真实对象的Save()方法中拥有的任何内容.

It is just a guess but Is Moq overriding the Save() method with it's own version which ignores anything I have inside the real object's Save() method.

推荐答案

您遇到了这个问题,因为您在嘲笑要测试的内容.这没有道理.

You are having this problem because you are mocking what you are testing. This doesn't make sense.

您是正确的,Moq将用自己的方法替换您的方法的实现.原因是您应该使用Moq来模拟正在测试的调用中的内容,而不是正在测试的类.

You are correct that Moq will replace the implementation of your method with its own. The reason is you are supposed to use Moq to mock things the class you are testing calls, not the class you are testing itself.

如果您的代码是按以下方式设计的,则此测试将是适当的:

This test would be appropriate if your code were designed thusly:

public class ClassA
{
    BusinessLogicClass bl;
    public ClassA(BusinessLogicClass bl)
    {
         this.bl = bl;
    }

    public void Save()
    {
        bl.ShouldBeCalled();
    }
}

public class BusinessLogicClass
{
    public virtual void ShouldBeCalled()
    {
        //This should get executed
    }
}

这是该方法的正确测试:

And here is the correct test of that method now:

[TestFixture]
public class ClassA_Test
{
    [Test]
    public void Save_ShouldCallShouldBeCalled()
    {
        //Arrange
        var mockBLClass = new Mock<BusinessLogicClass>();
        mockBLClass.Setup(x => x.ShouldBeCalled()).Verifyable();

        //Act    
        ClassA classA = new ClassA(mockBLClass.Object);
        classA.Save();

        //Assert
        mockBLClass.VerifyAll();
    }
}

这里的主要课程是模拟/存根测试需要运行的内容,而不是测试本身.

The key lesson here is that you mock/stub what your test needs to run, not what you are testing itself.

希望这会有所帮助, 安德森

Hope this helps, Anderson

这篇关于如何使用Moq调用类中的另一种方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆