且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用Moq的,以确定是否一个方法被称为

更新时间:2023-02-22 13:17:45

您可以看到,如果在一些方法你嘲笑被称为使用验证,如:

You can see if a method in something you have mocked has been called by using Verify, e.g.:

static void Main(string[] args)
{
        Mock<ITest> mock = new Mock<ITest>();

        ClassBeingTested testedClass = new ClassBeingTested();
        testedClass.WorkMethod(mock.Object);

        mock.Verify(m => m.MethodToCheckIfCalled());
}

class ClassBeingTested
{
    public void WorkMethod(ITest test)
    {
        //test.MethodToCheckIfCalled();
    }
}

public interface ITest
{
    void MethodToCheckIfCalled();
}

如果该行留下评论说,当你调用验证它会抛出一个MockException。如果没有被注释它会通过。

If the line is left commented it will throw a MockException when you call Verify. If it is uncommented it will pass.