Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6682141
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:40:17+00:00 2026-05-26T04:40:17+00:00

It came to my attention lately that you can unit test abstract base classes

  • 0

It came to my attention lately that you can unit test abstract base classes using Moq rather than creating a dummy class in test that implements the abstract base class. See How to use moq to test a concrete method in an abstract class? E.g. you can do:

public abstract class MyAbstractClass 
{
    public virtual void MyMethod()
    {
        // ...    
    }
}

[Test]
public void MyMethodTest()
{
    // Arrange            
    Mock<MyAbstractClass> mock = new Mock<MyAbstractClass>() { CallBase = true };

    // Act
    mock.Object.MyMethod();

    // Assert
    // ... 
}

Now I was wondering if there was a similar technique to allow me to test protected members without having to create a wrapper class. I.e. how do you test this method:

public class MyClassWithProtectedMethod
{
    protected void MyProtectedMethod()
    {

    }
}

I’m aware of the Moq.Protected namespace, however as far as I can see it only allows you to setup expectations with e.g.

mock.Protected().Setup("MyProtectedMethod").Verifiable();

I’m also aware that the obvious answer here is “don’t test protected methods, only test public methods”, however that’s another debate! I just want to know if this is possible using Moq.

Update: below is how I would test this normally:

public class MyClassWithProtectedMethodTester : MyClassWithProtectedMethod
{
    public void MyProtectedMethod()
    {
        base.MyProtectedMethod();
    }
}

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T04:40:17+00:00Added an answer on May 26, 2026 at 4:40 am

    For starters, there’s no point in unit testing an abstract method. There’s no implementation! You may want to unit test an impure abstract class, verifying that the abstract method was called:

    [Test]
    public void Do_WhenCalled_CallsMyAbstractMethod()
    {
        var sutMock = new Mock<MyAbstractClass>() { CallBase = true };
        sutMock.Object.Do();
        sutMock.Verify(x => x.MyAbstractMethod());
    }
    
    public abstract class MyAbstractClass
    {
        public void Do()
        {
            MyAbstractMethod();
        }
    
        public abstract void MyAbstractMethod();
    }
    

    Note that I set CallBase to turn this into a partial mock, in case Do was virtual. Otherwise Moq would have replaced the implementation of the Do method.

    Using Protected() you could verify that a protected method was called in a similar manner.

    When you create a mock with Moq or another library, the whole point is overriding implementation. Testing a protected method involves exposing existing implementation. That’s not what Moq is designed to do. Protected() just gives you access (presumably through reflection, since it’s string-based) to override protected members.

    Either write a test descendant class with a method that calls your protected method, or use reflection in the unit test to call the protected method.

    Or, better yet, don’t test protected methods directly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Today it came to my attention that a combination of jQuery selectors and the
Now that I learned to use Observers two questions came to my attention: 1
It came to my attention that there a several ways to compare strings in
I was just roaming through the API and it came to my attention that,
Came across the following MS Unit Test: [TestMethod] public void PersonRepository_AddressCountForSinglePerson_IsNotEqualToZero() { // Arrange.
Came across this error today. Wondering if anyone can tell me what it means:
Came by a curious case today, that got me thinking about how the object
I came across a printed article by Bertrand Meyer where he states that tests
I came across a controller in an older set of code (Rails 1.2.3) that
This came up in Hidden features of Python , but I can't see good

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.