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

  • Home
  • SEARCH
  • 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 3273192
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:54:22+00:00 2026-05-17T18:54:22+00:00

I have a class that inherits from an abstract base class. I am trying

  • 0

I have a class that inherits from an abstract base class. I am trying to verify that a specified protected method in the base class is called twice and I’d like to verify that the parameters passed are specific values (different for each call).

I was hoping that I’d be able to use Protected with either Expect or Verify, but seemingly I’ve missed what can be done with these methods.

Is what I’m attempting possible with moq?

UPDATE:
An example of what I’m trying to do:

class MyBase
{
    protected void SomeMethodThatsAPainForUnitTesting(string var1, string var2)
    {
        //Stuf with file systems etc that's very hard to unit test
    }
}

class ClassIWantToTest : MyBase
{
    public void IWantToTestThisMethod()
    {
        var var1 = //some logic to build var 1
        var var2 = //some logic to build var 2
        SomeMethodThatsAPainForUnitTesting(var1, var);
    }
}

Essentially I want to test the way the variables var1 and var2 are created correctly and passed into SomeMethodThatsAPainForUnitTesting, so essentially I want to mock out the protected method, verify that it was called at least once and that the parameters were all passed correctly. If this was calling a method on an interface it would be trivial, but I’m coming unstuck with a protected method.

I can’t easily change the design as it’s brown field development and I’m not the only class calling the method.

  • 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-17T18:54:22+00:00Added an answer on May 17, 2026 at 6:54 pm

    No it’s isn’t possible.

    Tools like Moq and Rhino mocks work their magic by generating subclasses of the types you want to mock at run time. They generate all their “Verify” and “Expect” logic by overriding a member (in the case of virtual members) or implementing it (in the case of an interface) and inserting code to inspect or record the passed arguments and return the pre-canned response.

    So what can you do to get around this? Firstly if you could alter the base method to be virtual this would then allow you to test your method by creating a test harness like so:-

    class ClassIWantToTestHarness : ClassIWantToTest {
        public string Arg1 { get; set; }
        public string Arg2 { get; set; }
        public int CallCount { get; set; }
    
        protected override void SomeMethodThatsAPainForUnitTesting(var1, var2) {
            Arg1 = var1;
            Arg2 = var2;
                CallCount++;
        }
    }
    
    [Test]
    public void ClassIWantToTest_DoesWhatItsSupposedToDo() {
        var harness = new ClassIWantToTestHarness();
        harness.IWantToTestThisMethod();
        Assert.AreEqual("Whatever", harness.Arg1);
        Assert.AreEqual("It should be", harness.Arg2);
        Assert.IsGreaterThan(0, harness.CallCount);
    }
    

    If it’s not possible to alter the base class at all due to the code being so horribly brownfield you don’t want to get your shoes dirty, you could simply wrap the method in ClassIWantToTest as a protected virtual method and perform the same trick on that instead. Moq does have support for overriding protected virtual members (see the miscellaneous section here) – though personally I prefer manual test harnesses in this case.

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

Sidebar

Related Questions

I have a class (Descendant1) that inherits from a base class (BaseClass). An instance
Let's say I have an abstract base class that looks like this: class StellarObject(BaseModel):
I have an asp.net usercontrol (ascx) that inherits from an abstract class (that inherits
I have a collection of classes that inherit from an abstract class I created.
I have a class that inherits from ObservableCollection(Of MyObject) and the MyObject class handles
I have a class A and another class that inherits from it, B. I
if i have a .net 1.1 class that inherits from DictionaryBase and i am
I have a class Application that my global.asax inherits from. The class has this
I have a standard windows server that inherits from the ServiceBase class. On the
I have a created a class that inherits from the Manager class that will

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.