Moq - Non-overridable members may not be used in setup / verification expressions
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Understanding Moq Limitations: Non-Overridable Members in Setup/Verification Expressions
Moq is a powerful mocking library for .NET that is widely used in unit testing. It allows developers to create mock objects and define behavior in a fluent manner. However, one common issue developers encounter is the constraint around "non-overridable members" in setup/verification expressions. This article will delve into this topic, providing technical insights, examples, and suggestions for effective usage.
What are Non-Overridable Members?
In C#, a non-overridable member is a method, property, or event that cannot be overridden in a derived class. These are typically defined using the sealed modifier in a base class or are inherently non-virtual when defined. In contrast, overridable members are typically flagged with the virtual, abstract, or override keywords, allowing them to be redefined in derived classes.
Why Moq Cannot Use Non-Overridable Members
The Moq library relies on the ability to substitute or "mock" the behavior of members of a class. This is usually achieved through dynamic proxy generation whereby a derived proxy class is created at runtime, with overridden virtual methods. Since non-overridable members cannot be overridden, Moq cannot replace these methods in the proxy, leading to the error: "Non-overridable members may not be used in setup/verification expressions."
Technical Explanation
- Virtual Methods: Moq requires the ability to override members within the generated proxy class. Moq uses
Castle DynamicProxyinternally to create these dynamic proxies, which is only feasible forvirtualorabstractmembers. - Sealed and Static Members: These members are explicitly designed to be non-overridable. Moq cannot intercept calls to these members, as their behavior is fixed at compile-time.
- Internal Members: If Moq is used outside of the same assembly, and the internal members aren't accessed via
InternalsVisibleToattribute, Moq won't mock them.
Example of Non-Overridable Members
Consider the following class:
- Extract the logic to an interface or abstract base class with virtual methods that can be mocked.
- Use a decorator pattern to wrap and override the behavior of the methods you need to change.
- Although not generally recommended, in some cases, you can utilize partial mocks to call base class methods using
CallBase.

