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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:15:25+00:00 2026-05-28T13:15:25+00:00

I have a piece of code that I may not be able to change.

  • 0

I have a piece of code that I may not be able to change. Ideally I’d move the Publish method into another class. Mocking would become a trivial operation and I’d be good. Here’s a simplified example.

public class Entity
{
  private IController _controller;

  // Omitted: Constructor where IController is assigned

  public virtual int Process()
  {
    return Publish(_controller.DoSomeStuff());
  }

  public virtual int Publish(int val)
  {
    Console.WriteLine(val);  // This is actually a call to an external system.
    return val;
  }
}

What I want to do is call the real Process method and a mocked Publish method. The value of _controller will be mocked as well.

I tried using moq and doing:

var mock = new Mock<IController>();
mock.Setup(m => m.DoSomeStuff()).Returns(11);

var mock2 = new Mock<Entity_Accessor>();
mock2.CallBase = true;
mock2.Setup(a => a.Publish(It.IsAny<int>())).Returns(999);
mock2.Object._controller = mock.Object;

int result = mock2.Object.Process();
Assert.AreEqual(999, result);

When I run this test, the value of 11 is returned instead of 999. If I remove mock2.CallBase = true, the entire class is mocked which returns default values for every function.

Is it possible to mock just the Publish method in this case while retaining the original implementation of Process?

Is it possible to achieve the above with a different mocking framework that isn’t Typemock or Justmock (not sure if the customer will foot the bill for multiple copies of a mocking tool)?

Thank you!

  • 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-28T13:15:26+00:00Added an answer on May 28, 2026 at 1:15 pm

    If you are trying to test the functionality of the Process method you should not be using a mock, you should be using a real instance of the class and mocking the IController interface and mock DoSomeStuff to return an expected value and then verify that Process correctly handles that value.

    If you are simply trying to mock your Entity class, then there is no need to mock the IController. Just mock your methods on the Entity class and have them return what you want.

    Update:

    You shouldn’t need to mock part of the class you are testing. Maybe you should be passing in a reference to the external system so that you can mock it. Unless the code in Publish is the responsible party for directly calling the external system. This would be the recommended approach. However if the Publish method is responsible for calling the external system directly, then you need to take a different approach and do integration testing instead.

    Integration testing usually does not run fast in comparison to unit tests, and it requires that all system interacting be up and running. It also means that in most cases you won’t be mocking things. This is the nature of the beast and I would highly recommend that you not circumvent a process such as this.

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

Sidebar

Related Questions

I have a piece of code that may or may not fail, which I
I have this piece of code that does not work: public CartaoCidadao() { InitializeComponent();
I have a piece of code that the ARC converter turned into this... //
I have the following piece of code (that may be invoked multiple times or
I have this piece of code that works fine in subsonic 2.2, I migrated
I have a piece of code that load a very big image in memory.
I have a piece of code that takes several rows from a database and
I have a piece of code that look similar to this: <xsl:choose> <xsl:when test=some_test>
I have a piece of code that calls a WCF service that is hosted
If have a piece of code that gets some data from a sql database

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.