Moq - Non-overridable members may not be used in setup / verification expressions
Data Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.
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.
Related reading
- Moq, strict vs loose usage
- Moq verify with object parameter
- Most common C bitwise operations on enums
- Most efficient algorithm for merging sorted IEnumerableT
- MsTest - executing method before each test in an assembly
- Mutation not killed when it should be with a method with an auto-injected field
- Most efficient way to check for DBNull and then assign to a variable?
- Most efficient way to get default constructor of a Type

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.