Cant setup test container registration with a string name param

Nov 20, 2014 at 8:44 AM
hi,

my actual code looks something like this:

_container.Resolve<IProcess>(ProcessType.A.ToString());

and my definitions looks something like this:

container.RegisterType<IProcess, ProcessA>(ProcessType.A.ToString());
container.RegisterType<IProcess, ProcessB>(ProcessType.B.ToString());

During runtime it all works fine, but when i try to write a mock test such as:

var mockProcess = new Mock<IProcess>();
unityContainer.RegisterInstance(typeof(IProcess), mockProcess);

and the calling code tries to execute:

var process = _container.Resolve<IProcess>(ProcessType.A.ToString());

it doesnt work and cant resolve the type.
How can i setup my test container registration so this works?

any help appreciated.
Dec 2, 2014 at 1:29 AM
It looks like the issue is that in your test case you are registering the instance with no name (default registration) but you are trying to resolve using a name so the container cannot locate a registration by that name.
Dec 2, 2014 at 1:59 AM
randylevy wrote:
It looks like the issue is that in your test case you are registering the instance with no name (default registration) but you are trying to resolve using a name so the container cannot locate a registration by that name.
No worries, i ended up moving the resolving of the interface type out of the method and injected it into the constructor instead. Was purely for testability and cleaned up some code in the process :-)
Marked as answer by milsnips on 12/1/2014 at 6:59 PM
Dec 2, 2014 at 2:03 AM
OK, constructor injection sounds like a better approach. :)