How to verify that method was NOT called in Moq?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Mocking is a critical part of unit testing, especially when it comes to testing interactions between a method under test and its dependencies. In the .NET world, Moq is a popular library that enables developers to create test doubles, specifically mocks, for dependencies that the system under test (SUT) interacts with. One common aspect of unit testing with mocks is ensuring that certain methods on the mock were not called during the execution of a test. This article provides a detailed explanation of how to verify that a method was not called using Moq.
Understanding Moq and Setup Verification
Moq Basics
Moq allows you to create and configure mocks for your abstract dependencies, typically an interface or an abstract class. Using Moq, developers can specify what should happen when methods or properties of these mocks are accessed. The Moq framework offers two main components related to setting up and verifying the behavior of mocks:
- Setup: Used to specify how a mock should behave when a method or property is accessed.
- Verify: Used to confirm that a method was called on a mock in a specific way or a specific number of times.
Importance of Verifying Calls
When writing tests, it might be necessary to ensure that a method on your mock dependency was not called. For instance, you might want to check that a certain action doesn't trigger unnecessary operations.
Implementation in Moq
Verifying a Method was Not Called
In Moq, verifying that a method was not called can be done using the Verify method in conjunction with the Times.Never argument. Here's the syntax:
Here, MethodName is the method of your mock object that you want to assert was not called.
Detailed Example
Consider an interface IService with a method ProcessOrder:
In the above setup, OrderProcessor calls ProcessOrder only if cancelOrder is false. Here's how you can test that ProcessOrder was not called when cancelOrder is true:
Understanding It.IsAny
The It.IsAny<T>() method is a placeholder for any argument of type T. It is useful when the specific value of the argument does not matter for the test scenario.
Additional Considerations
Limiting to Specific Setups
If you have more complex setups involving multiple overloads or lambda expressions, you can further refine what "not called" means by setting up more constrained Verify expressions. Moq is flexible and allows specifying parameters precisely:
Debugging Verification Failures
When a Verify assertion fails, Moq throws an Moq.MockException. Debugging these exceptions involves checking:
- Incorrect method name or arguments in the
Verifycall. - Ensuring that the scenario under test actually gets the mock injected.
- Ensuring the method logic correctly guards the call as expected.
Summary Table
| Concept | Explanation |
| Setup | Configures the mock's behavior. |
| Verify | Asserts how/if a method was called. |
Times.Never | Ensures a method was not called. |
It.IsAny<T>() | Placeholder for method arguments. |
Debugging MockException | Check method name, argument correctness. |
Conclusion
Verifying that methods were not called is as crucial as ensuring they were called in specific scenarios when testing interactions. Using Moq's Verify method with Times.Never, you can effectively assert negative occurrences in your unit tests. This capability is essential for writing thorough and meaningful tests, confirming that your application logic behaves as expected under all circumstances.

