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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:20:36+00:00 2026-05-11T21:20:36+00:00

So I have a helper method that looks something like the following: private D

  • 0

So I have a helper method that looks something like the following:

private D GetInstanceOfD(string param1, int param2)
{
    A a = new A();
    B a = new B();
    C c = new C(a,b, param1);

    return new D(c, param2);
}

It’s just a convenient helper method for which I can call to grab a particular object that I need rather then to remember which dependencies I need to hook up to get the object I require.

My first question here is: should methods like these be tested? The only reason I can think of to want to test these type of methods would be to make sure that the correct dependencies are used and are set up correctly.

If the answer to the first question is yes, my second is: how? I am currently using NUnit and RhinoMocks and am trying to work out how this method has to be refactored to be testable (well, and whether something like this should be tested!); dependency injection obviously would not work here as this method actually creates the dependencies!

Or is using this method bad practice and I should be doing something like the following:

D d = new (new C(new A(), new B(), "string"), 1024);
  • 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-11T21:20:36+00:00Added an answer on May 11, 2026 at 9:20 pm

    First, the method is private and, so, normally I would say that testing is probably not necessary for that method directly. Even if it were a helper method for real code, instead of a helper for your tests, you may want to only test it indirectly through the public interface.

    Second, if you do want to test it — and perhaps anyway — you should consider using factories to do the object creation rather than directly instantiating the objects. Using a factory, in conjunction with interfaces, will allow you to abstract the dependencies out. Simply inject the factory into the constructor of the class containing this method and use it as a property within this method to create the proper objects. If you go this route, you may also find that the factory is also the place for the logic to create D.

    This would definitely be the direction I would take if the code were production code. Example below:

    public class ObjectFactory 
    {
         public virtual A CreateA() { return new A(); }
         public virtual B CreateB() { return new B(); }
         public virtual C CreateC( string str ) { return new C( CreateA(), CreateB(), str );
         public virtual D CreateD( string str, int num )
         {
             return new D( CreateC( str ), num );
         }
    }
    
    
    public class Foo
    {
        private ObjectFactory ObjectFactory { get; set; }
    
        public Foo() : this(null) { }
        public Foo( ObjectFactory factory )
        {
             this.ObjectFactory = factory ?? new ObjectFactory();
        }
    
        public void Bar()
        {
            D objD = this.ObjectFactory.CreateD( param1, param2 );
        }
    }
    
    [TestMethod]
    public void TestBar()
    {
         var factory = MockRepository.GenerateMock<ObjectFactory>();
         var d = MockRepository.GenerateMock<D>();
         factory.Expect( f => f.CreateD( "abc", 10 ) ).Return( d );
    
         var foo = new Foo( factory );
    
         foo.Bar();
    
         factory.VerifyAllExpectations();
         d.VerifyAllExpectations();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an observer which looks like this: class CommentObserver < ActiveRecord::Observer include ActionView::Helpers::UrlHelper
I'm trying to write a helper class that represents fields on an object. The
In Rails 3, I am having a problem accessing a helper method from within
intHi, Pretty new to LINQ. I have a Ratings table. A User adds a
I have a bunch of different methods that are not supposed to run concurrently,
I'm hoping to implement something like all of the great plugins out there for
I want to create a generalized helper method for LoadFromXML loading and validation. If
I have a helper function which allows me to call functions in a different
I have a Rails 3 app with a presenter that lives in lib .
I'm working with reflection read and writing objects. I have the problem that I

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.