Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Verify a method call using Moq

People also ask

What is verify in Moq?

Verifies that all verifiable expectations have been met.

Can you mock a private method Moq?

Moq supports mocking protected methods. Changing the methods to protected , instead of private , would allow you to mock their implementation.

Is Moq a testing framework?

The Moq framework is an open source unit testing framework that works very well with .

What is times once in Moq?

AtLeastOnce - Specifies that a mocked method should be invoked one time as minimum. AtMost - Specifies that a mocked method should be invoked times time as maximum. AtMostOnce - Specifies that a mocked method should be invoked one time as maximum.


You're checking the wrong method. Moq requires that you Setup (and then optionally Verify) the method in the dependency class.

You should be doing something more like this:

class MyClassTest
{
    [TestMethod]
    public void MyMethodTest()
    {
        string action = "test";
        Mock<SomeClass> mockSomeClass = new Mock<SomeClass>();

        mockSomeClass.Setup(mock => mock.DoSomething());

        MyClass myClass = new MyClass(mockSomeClass.Object);
        myClass.MyMethod(action);

        // Explicitly verify each expectation...
        mockSomeClass.Verify(mock => mock.DoSomething(), Times.Once());

        // ...or verify everything.
        // mockSomeClass.VerifyAll();
    }
}

In other words, you are verifying that calling MyClass#MyMethod, your class will definitely call SomeClass#DoSomething once in that process. Note that you don't need the Times argument; I was just demonstrating its value.