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

The Archive Base Latest Questions

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

I am mocking in this sort of situation: class A { public IB B

  • 0

I am mocking in this sort of situation:

class A
{
    public IB B {get; set;}
}

interface IB
{
  //methods
}

class B : IB
{
  //IB methods
}

public SimpleMockTest()
{
   var mockIB = new Mock<IB>();
   var a = new A{B=mockIB.Object};
   //do stuff with A, then verify methods on IB were called       
}

public TheKindOfTestIWantToDo()
{
   var actualB = new B();
   var mockIB = new Mock<IB>();
   var splitter = new Splitter<IB>(actualB, mockIB.Object); //I need something like this
   var a = new A{B=splitter};
   //do stuff with A, methods invoked on splitter IB get passed to both supplied instances
   //of IB (actualB, and the mockIB). Allowing me to verify the calls as well as have the methods invoked on the actual B object.
   //OR:
   var mockIB2 = new Mock<IB>(actualB); //behaviour is dictated by the actual, but allows verification
   var a2 = new A{B=mockIB2.Object};
}

So I am after some sort of proxy factory that would genericly support an interface and call the same method on multiple interfaces. Of course where a method returns a value, each would need to return the same value, or maybe one takes precident.

I want to effectively snoop on interface calls during the test, but without the mock being the end of the line.

Am using Moq if it makes any difference.

  • 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-05T23:49:08+00:00Added an answer on June 5, 2026 at 11:49 pm

    As far as I know, there isn’t a way to do this for all methods on actualB.

    I think you want something like:

    public TheKindOfTestIWantToDo()
    {
       var actualB = new B();
       var mockIB = new Mock<IB>();
       mockIB.Setup(b => b.Foo()).Returns(() => actualB.Foo())
       var a = new A { B = mockIB };
       //do stuff with A, methods invoked on splitter IB get passed to both supplied instances
       //of IB (actualB, and the mockIB). Allowing me to verify the calls as well as have the methods invoked on the actual B object.
    }
    

    You should probably rethink your unit testing strategy, though. The behavior of A should be testable without relying on the behavior of B. Try having mockIB return specific values, and then write a different set of tests for B.

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

Sidebar

Related Questions

//class = Person public string Name { get; internal set; } I have an
Suppose I have this: @Transactional(rollbackFor = NotificationException.class) public interface PersonManagerService { public void addPerson(Person
I have this class called Table: class Table { public string Name { get
I am writing a small mocking class to do some tests. But this class
Quite new to this mocking thing, I have a couple of questions. Correct me
I have a class like this: public final class Foo { public native int
I am still new to mocking and I am having trouble with this code:
Consider this code: private readonly Dictionary<Type, Component> _components = new Dictionary<Type, Component>(); public Component
I'm pretty new to mocking so this might be something I'm just not picking
New to Moq and Mocking in general. Testing a class that has a generic

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.