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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T19:11:39+00:00 2026-06-02T19:11:39+00:00

Update I want to find out if the factory should be unit tested based

  • 0

Update

I want to find out if the factory should be unit tested based on its usage of method, because if I want to test it, I need to register type using IoC container, Unity used in my case. If I mock the factory, it is not actuall testing the factory method.

Below is a factory class that creates instances of type based on its parameter.

public class CarFactory
    {

        public ICar CreateCar(string CarType)
        {
            ICar Car;

            switch (CarType)
            {
                case RepositoryType.Car1:

                    Car = Ioc.ContainerWrapper.Resolve<Car1>();

                    break;
                case RepositoryType.Car2:       
                    Car = Ioc.ContainerWrapper.Resolve<Car2>();

                    break;

                default:

                    Car = Ioc.ContainerWrapper.Resolve<Car3>();

                    break;

            }

            return Car;
        }

    }


      class Car1 
    {

        private readonly IRepository1 _IRepository1;

        public Car1(IRepository1 repository1)
        {
            _IRepository1 = repository1;

        }
}
  • 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-02T19:11:40+00:00Added an answer on June 2, 2026 at 7:11 pm

    You are on the right path, from my point of view.

    The purpose of a factory is to create an object so they are allowed to call the Service Locator (Yes, you are still using the Service Locator anti-pattern but you are moving it from your domain to just a factory)

    Reference:

    https://github.com/ninject/ninject.extensions.factory/wiki

    Back to your original question, I believe you should test your factory to guarantee that you are getting the correct object depending on the parameter used, there is a debate on if this is a unit test or any kind of integration test, in my opinion it is still a unit test following this Martin Fowler article:

    http://martinfowler.com/articles/mocksArentStubs.html#ClassicalAndMockistTesting

    This is how I would test it:

    IRisk myRisk = new RiskFactory().CreateGoldRisk("Risk1");
    
    myRisk.Should().NotBeNull().And.BeOfType<Risk1>();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Dont worry we dont need to find out any bug in this code. Its
i want to update my sqlite database but i cannot find the way to
i have ios application with xcdatamodeld, i want update some data so need a
I want to find out what methods or tools in general can be used
I need to find out how to perform some action (flush cache) when an
I'm looking for a way to find out if there are uncommited INSERT, UPDATE
i want to find out if the user is currently using more than one
Can I find out when the last INSERT, UPDATE or DELETE statement was performed
I need help on my code igniter app..i can't seem to find out what's
I am using Visual Studio 2010 Ultimate. I want to find out which methods

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.