Coding⏱️ 2 min read📅 2026-05-31

How to Fix: Why am I getting an Exception with the message "Invalid setup on a non-virtual (overridable in VB) member..."?

Mock a non-virtual method in C# unit testing

Quick Answer: Use the “It’s a new feature’ approach to mock the method, or use a library like Moq to create a mock object for the method.

The issue you're encountering is due to the non-virtual method call in your unit test. When using mock objects, it's crucial to ensure that the methods being mocked are virtual.

🛠️ Step-by-Step Verified Fixes

Method 1: Make the Method Virtual

  1. Step 1: Open your XmlCupboardAccess class and add the "virtual" keyword to the IsDataEntityInXmlCupboard method.

Method 2: Use a Mock Framework

  1. Step 1: Install a mocking library such as Moq or NSubstitute in your project.

✨ Wrapping Up

By applying one of these solutions, you should be able to resolve the Invalid setup on a non-virtual (overridable in VB) member exception and successfully run your unit tests.

Did this fix your problem?

If not, try searching for specific error codes.

🔍 Search Error Database

❓ Frequently Asked Questions