Why not actually just mock IUnityContainer
directly, instead of using a real UnityContainer?
From the point of view of unit testing the PerformUpload
method you should only assert that indeed both Resolve<>
and Upload
methods are called and with the corresponding parameters. Not the actual internals of how Unity resolves.
In fact you could even remove the dependency on the IUnityContainer
and have your constructor receive a factory method instead, that you can register in Unity.
I tend to avoid such dependencies, and declare the factory directly as Func<string, IUploadModule>
.
The following example is untested but something like the following may work:
container.RegisterType<Func<string, IUploadModule>>(new InjectionFactory(c =>
new Func<string, IUploadModule>(name =>
c.Resolve<IUploadModule>(name)
)
)
);
This is an additional registration. You would also have to have your uploaders registered just as you have them now.
With that registered, you can have your constructor use Func<string, IUploadModule>
as a parameter:
public Uploader(Func<string, IUploadModule> factory)
{
_factory = factory;
}
And invoke it in the following manner to get your uploader:
_factory("Patients");