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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:55:09+00:00 2026-05-30T18:55:09+00:00

I have the following code public ClassToTest : IClassToTest { private readonly DBRepository rep;

  • 0

I have the following code

public ClassToTest : IClassToTest
{
    private readonly DBRepository rep;

    public bool MethodA()
    {
        //Some logic
        var result=MethodB();
        //Do some logic against result;
    }
    public ResultType MethodB()
    {
        return Rep.GetResult();
    }
}

If I want to Unit testing MethodA, what is the best practice to test the interaction between MethodA and MethodB? I am thinking to test MethodA like testing MethodB by mocking Database dependency Rep, just like MethodA has the following implementation

    public bool MethodA()
    {
        //Some logic
        var result=Rep.GetResult();
        //Do some logic against result;
    }

But it is not intuitive by checking the logic in the code and the test method. I am looking for a solution similar to the one mentioned here for Java.

unit testing composite service methods

But It is not working for C#.

One extra question, What if MethodB is private, does it make any difference for the Unit testing strategy?

Update: prefer not to change the structure of the class. like not making MethodB as virtual or Move the MethodB out of the class into another test

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-30T18:55:11+00:00Added an answer on May 30, 2026 at 6:55 pm

    You don’t want to test the interaction between MethodA and MethodB, you want to test that MethodA will return the expected bool result, given some context.

    The fact that MethodA calls MethodB is not germaine to this test; but the fact that Rep.GetResult() will at some point be called is.

    As you mentioned, you can mock the dependency Rep, so it won’t matter whether MethodB is public or private.

    1. Mock and inject the dependency
    2. Call MethodA
    3. Assert against the result
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: public partial class queryTerm : System.Web.UI.UserControl { private static
I have the following code public void SerializeToStream(Stream stream) { var xml = //
I have the following code: public OTestTable GetTestCode(Func<TestTable, bool> whereClause) { return CoreContext.TestTables.Where(whereClause).Select(TestTableMap.DataToObject).FirstOrDefault(); }
I have the following code: public class Events extends ListActivity { //... private static
I have the following code: public IList<ProductionRuns> List() { var Shows3Months = (from s
I have the following code: public class Search { private Desktop desktop = new
I have the following code :- public class Test5 { private int value ;
I have the following code: public class NavigationPath { private string menuItems = <li>
I have the following code: public partial class Main : Form { private delegate
I have the following code: public void DeleteAccountsForMonth(int year, int month) { var result

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.