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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T11:32:02+00:00 2026-05-15T11:32:02+00:00

I have a class Foo that uses another class Bar to do some stuff.

  • 0

I have a class Foo that uses another class Bar to do some stuff.

I’m trying to do test driven development, and therefore am writing unit tests for Foo to make sure it calls the appropriate methods on Bar, and for this purpose I’m using dependency injection and mocking Bar (using Rhino Mocks).

E.g. (in C#):

class Foo
{
  private IBar bar= null;

  public Foo(IBar bar)
  {
    this.bar= bar;
  }

  public void DoSomething()
  {
    bar.DoIt();
  }
}

class FooTests
{
  ...
  void DoSomethingCallsBarsDoItMethod()
  {
    IBar mockBar= MockRepository.GenerateMock<IBar>();
    mockBar.Expect(b=>b.DoIt());    
    Foo foo= new Foo(mockBar);
    foo.DoSomething();
    mockBar.VerifyAllExpectations();
  }
}

This all seems to be fine, but I actually want Bar to be configured with a particular parameter, and was going to have this parameter passed in via Foo’s constructor.
E.g. (in C#):

public Foo(int x)
{
  this.bar = new Bar(x);
}

I’m not sure which is the best way to change this to be more easily testable. One options I can think of involves moving the initialization of Bar out of its constructor, e.g.

public Foo (IBar bar, int x)
{
  this.bar= bar;
  this.bar.Initialize(x);
}

I feel that this is making Bar harder to use though.

I suspect there may be some kind of solution that would involve using an IoC container configured to inject Foo with a mock IBar and also provide access to the created mock for expectation validation, but I feel this would be making Foo unnecessarily complex. I’m only using dependency injection to allow me to mock the dependencies for testing, and so am not using IoC containers at present, just constructing dependencies in a chained constructor call from the default constructors (although I realize this is creating more untested code) e.g.

public Foo() :
  this(new Bar())
{
}

Can anyone recommend the best way to test dependent object construction/initialization?

  • 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-15T11:32:03+00:00Added an answer on May 15, 2026 at 11:32 am

    It seems to me that you are letting an implementation detail leak through the API design. IBar shouldn’t know anything about Foo and its requirements.

    Implement Bar with Constructor Injection just like you did with Foo. You can now make them share the same instance by supplying it from the outside:

    var x = 42;
    IBar bar = new Bar(x);
    Foo foo = new Foo(bar, x);
    

    This would require your Foo constructor to look like this:

    public class Foo
    {
        private readonly int x;
        private readonly IBar bar;
    
        public Foo(IBar bar, int x)
        {
            if (bar == null)
            {
                throw new ArgumentNullException("bar");
            }
    
            this.bar = bar;
            this.x = x;
        }
    }
    

    This allows Foo to deal with any implementation of IBar without letting the the implementation details leak through. Separating IBar from x can be viewed as an implementation of the Interface Segregation Principle.

    In DI Container terminology, x can be viewed as being Singleton-scoped (not to be confused with the Singleton design pattern) because there is only a single instance of x across many different components. The instance is shared and it is strictly a lifetime management concern – not an API design concern.

    Most DI Containers have support for defining a service as a Singleton, but a DI Container is not required. As the above example shows, you can also wire shared services up by hand.

    In the cases where you don’t know the value of x until run-time, Abstract Factory is the universal solution.

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

Sidebar

Related Questions

I have written some code that uses attributes of an object: class Foo: def
I have an entity class Foo foo that contains Collection<Bar> bars . I've tried
Suppose I have a class Baz that inherits from classes Foo and Bar ,
I have two classes Foo and Bar that Bar extends Foo as below: class
I'm using NetBeans as my IDE. Whenever I have some code that uses another
I have a class (foo) that contains a vector. If i try iterating over
How to select next n hidden elements that have class foo using jQuery?
I have a Rack application that looks like this: class Foo def initialize(app) @app
I have several categories that I use in my Grails plugin. e.g., class Foo
Let's say I have a bunch of classes that look something like... class Foo{

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.