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;
}
}
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: