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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T04:02:47+00:00 2026-05-31T04:02:47+00:00

I want to use one instance of a mock library I am using in

  • 0

I want to use one instance of a mock library I am using in a test class, however, for some tests, I may want to mock one of the member functions to do/return mocked behavior/returned value; for other tests, I may want the unmocked (native) functionality of the library. Is there a way to swap between Setup in one instance (mocked behavior), and “UNSetup” (unmocked behavior) in another?

  • 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-31T04:02:49+00:00Added an answer on May 31, 2026 at 4:02 am

    There isn’t any built-in mechanism to do that, but partial mocks kind of let you do the same (with some limitations). Parial mock allows you to mock concrete implementation of the interface as opposed to interface alone, like this:

    var partialMock = new Mock<ServiceImplementation>();
    

    Limitations are that all the methods you possibly want to mock need to be virtual, otherwise Moq cannot intercept them:

    public class ServiceImplementation
    {
        public virtual int SomeMethod()
        {
            return 5;
        }
    
        public virtual int SomeOtherMethod()
        {
            return SomeMethod()*2;
        }
    }
    
    
    var partialMock = new Mock<ServiceImplementation>();
    // we stub one method
    partialMock.Setup(m => m.SomeMethod()).Returns(3);
    // and use other's real implementation
    var value = partialMock.Object.SomeOtherMethod();
    

    Problem of course lies in the virtuality; in case you can’t make your members virtual this obviously won’t work. There’s minor workaround though – use real implementation as parts of stub setup:

    // note we base our stub on interface now
    var implementation = new ServiceImplementation();
    var mock = new Mock<IServiceImplementation>();
    // we call real implementation as part of return setup
    mock.Setup(m => m.SomeMethod()).Returns(implementation.SomeMethod());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We are two students who want to use one-class svm for dectection of summary
I want to use the values of JTextFields from one class in another class
I use CopyOnWriteArraySet to store one instance of a custom class, which looks like
I Mean that i want to use one activity into another activity, Like class
I need to make one instance of some class - and this one instance
I'm writing a webpage where I want to use one font for normal text
I have a few pages and want that they use one style. See in
I want to use Paypal Payment Standard(the one jumps to paypal to pay and
I want to use multiple queries in one list view.In my list view each
I want to use many bars like this one in my app (i don't

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.