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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:40:59+00:00 2026-05-27T04:40:59+00:00

The problem Let’s draw a picture of the situation: I have a SUT. (a

  • 0

The problem

Let’s “draw” a picture of the situation:

  • I have a SUT. (a good thing to have 😛 )
  • I can inject some dependencies on my SUT.
  • In a method I do a: new OtherClass(ParametersObtainedFromDependancies).
  • I want to unit test that method in my SUT without handling the complexity of creating mock object that make sense for OtherClass to work correctly. This will imply to test OtherClass.

My solution to this was a factory method using a delegate:

public Func<OtherClass,Param1Type> GetOtherClass = 
       (param1) => new OtherClass(param1);

The bad: it’s public. You could think on it as an optional dependency that you can override if need. But anyway, public smells.

The good: I don’t need to create a MyTestSUT that will override this method, or even use a mock on the SUT to override that method.

Question

Is there any better solution? Is this correct?

  • 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-27T04:41:00+00:00Added an answer on May 27, 2026 at 4:41 am

    Why have you made it a public field? It sounds like it’s basically a dependency like any other: you want something which will provide you with an OtherClass when you give it some other values. Why not just make it a constructor parameter like other dependencies (or however you’re doing the rest of your dependency injection)?

    You can always provide a constructor overload without this parameter which delegates to the longer one, if you want to. That’s how I normally provide “default” dependency implementations.

    To put it another way: how is this logically different from (say) an IAuthenticator interface which, when provided with a username and password will return a UserProfile? You happen to be using a delegate as a short-cut to avoid declaring the interface, but that’s just a detail IMO. You may find that as time goes on, you actually want to put more smarts in the “providing” part – e.g. caching.

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

Sidebar

Related Questions

I would like to have some suggestions for the following problem: Let's say you
I have problem to let user know about exceptions occurred in PrimeFaces LazyDataModel#load method.
I have a question on making a good subquery for this problem Let's say
The problem: let's say I have two tables Client , and Product , in
The underlying problem - let's say my documents have categories and timestamps . If
Let me show what I need first so you can understand my problem. I
Here is my problem: I have an array of model class(Let's say, 'addressModel' with
This is a design problem I am facing. Let's say I have a cars
My problem Let's say I want to hold my messages in some sort of
I am new to ADO.net and have this problem: Let's assume I have these

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.