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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:32:44+00:00 2026-05-27T22:32:44+00:00

In my unit tests, I use things like AssemblyInitialize , ClassInitialize and TestInitialize to

  • 0

In my unit tests, I use things like AssemblyInitialize, ClassInitialize and TestInitialize to configure my tests. In AssemblyInitialize I initialize some singleton factories for creating services, a unit of work and repositories (all trough Unity/Dependency Injection). In my TestInitialize I clear the state they have to make sure each test can run independently.

Trough Stackoverflow I came around the following articles: Writing Testable Code and How to Think About the “new” Operator with Respect to Unit Testing. I have to say they changed how I think about some code I’ve written but my mind is still buzzing.

For example, take the following code:

public class MyPresenter : BasePresenter<IMyView>
{
    public MyPresenter(IMyView view) 
        : base(view)
    {
    }

    public void PrepareView()
    {
        using (IMyService service = ServiceFactory.Instance.CreateService<IMyService>())
        {
            View.Data = service.GetData();
        }
    }
}

Which is used in an aspx page like this:

Presenter = new MyPresenter(this);
if (!IsPostback)
{
    presenter.PrepareView();
}

How should I change this type of kind with the previous articles in mind? Should I pass a service instance to the PrepareView method? But then my ASPX page has to know about services and factories and stuff and dispose of the service after it’s used.

What should I do with my Singleton factory? Make it a property of the class and inject it trough Unity?

  • 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-27T22:32:45+00:00Added an answer on May 27, 2026 at 10:32 pm

    I found this kind of resolving dependencies inelegant, Take a look at the way Ninject wire the dependencies or inject them.

    You can see the difference, Service Locator is an antipattern, reading this blog also makes more clear the essential purpose and practices for dependency injection in .NET

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

Sidebar

Related Questions

So, I've started to create some Ruby unit tests that use Selenium RC to
I tried recently to use NAnt (beta 0.86.2962.0) to run some unit tests compiled
I'm trying to unit-test some classes that make use of a Singleton class whose
Within our unit tests we use plain ADO.NET (DataTable, DataAdapter) for preparing the database
Many people use Mock Objects when they are writing unit tests. What is a
For managing unit tests in Visual Studio, I use the Test List Editor. There's
I started to use AutoFixture http://autofixture.codeplex.com/ as my unit tests was bloated with a
I have a situation where I need to write some unit tests for some
I want to run unit tests (Junit) on some model classes that typically will
Is there a way to use mocks or fakes in your unit tests without

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.