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 8084827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T17:55:00+00:00 2026-06-05T17:55:00+00:00

I want to mock only the GetValue method of the following class, using Moq:

  • 0

I want to mock only the GetValue method of the following class, using Moq:

public class MyClass
{
    public virtual void MyMethod()
    {
        int value = GetValue();
        Console.WriteLine("ORIGINAL MyMethod: " + value);
    }

    internal virtual int GetValue()
    {
        Console.WriteLine("ORIGINAL GetValue");
        return 10;
    }
}

I already read a bit how this should work with Moq. The solution that I found online is to use the CallBase property, but that doesn’t work for me.

This is my test:

[Test]
public void TestMyClass()
{
     var my = new Mock<MyClass> { CallBase = true };
     my.Setup(mock => mock.GetValue()).Callback(() => Console.WriteLine("MOCKED GetValue")).Returns(999);
     my.Object.MyMethod();
     my.VerifyAll();
 }

I would expect that Moq uses the existing implementation of MyMethod and calls the mocked method, resulting in the following output:

ORIGINAL MyMethod: 999
MOCKED GetValue

but that’s what I get :

ORIGINAL GetValue
ORIGINAL MyMethod: 10

and then

Moq.MockVerificationException : The following setups were not matched: MyClass mock => mock.GetValue()

I got the feeling, that I misunderstood something completely. What am I missing here?
Any help would be appreciated

  • 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-06-05T17:55:02+00:00Added an answer on June 5, 2026 at 5:55 pm

    OK, I found the answer to this in another question: How to Mock the Internal Method of a class?. So this is a duplicate and can be closed.

    Nevertheless, here’s the solution:
    just add this line to the Assembly.config of the project you want to test:

    [assembly: InternalsVisibleTo("DynamicProxyGenAssembly2")] // namespace in Moq
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Assume the following entity classes: public class Player { public virtual int ID {
I want to mock a particular method of a class, problem i am facing
I have the following class: I want to ensure the class url is only
I have the following class: public abstract class AbstractParent { static String method() {
I want to make a mock installer using NSIS so we can demo what
I want to mock a ServiceContract. The problem is that Moq (and Castle Dynamic-Proxy)
I have the following Logger I want to mock out, but to validate log
Given the Thread class with it current method. Now inside a test, I want
Is it possible to have an actual object of a class and only mock
When using jmock, you can mock a concrete class if you set the class

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.