Registering NUnit DynamicMock instances in UnityContainer

I'm a little new to Unity and dependency. I am trying to write a unit test that looks something like this:

[Test] public void Test() { UnityContainer container = new UnityContainer(); DynamicMock myMock = new DynamicMock(typeof(IMyInterface)); container.RegisterInstance(typeof(IMyInterface), myMock.MockInstance); //Error here // Continue unit test... } 

When this test runs, the container throws an ArgumentNullException inside the RegisterInstance method with the message Value cannot be null. Parameter name: assignmentValueType. Value cannot be null. Parameter name: assignmentValueType.

Top stack trace line at Microsoft.Practices.Unity.Utility.Guard.TypeIsAssignable(Type assignmentTargetType, Type assignmentValueType, String argumentName) .

Why can't I register MockInstance with UnityContainer and how do I get around this?

+6
dependency-injection ioc-container nunit mocking unity-container
source share
1 answer

I do not see it. I use NUnit 2.5.5.10112 and Unity 2.0 (which comes with EntLib, a separate release is not yet available).

Update: I just checked with 1.2 and I see your behavior. So this is a problem with 1.2.

 namespace UnityRepro { public interface IMyInterface { void Foo(); } public class Class1 { [Fact] public void Test() { UnityContainer container = new UnityContainer(); DynamicMock myMock = new DynamicMock(typeof(IMyInterface)); container.RegisterInstance(typeof(IMyInterface), myMock.MockInstance); //Error here Assert.NotNull(container.Resolve<IMyInterface>()); } } } 

Can I upgrade to Unity 2.0? If not, I will try to go deeper and find out what is really happening. This may be a limitation of 1.2 though.

+3
source share

All Articles