6

I'm somewhat new to Unity and dependency injection. I'm trying to write a unit test that goes 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 executes, the container throws an ArgumentNullException inside the RegisterInstance method with the message Value cannot be null. Parameter name: assignmentValueType.

The top line of the stack trace is at Microsoft.Practices.Unity.Utility.Guard.TypeIsAssignable(Type assignmentTargetType, Type assignmentValueType, String argumentName).

Why can't I register a MockInstance with the UnityContainer, and how do I work around this?

Phil
  • 6,561
  • 4
  • 44
  • 69
  • Related: http://stackoverflow.com/questions/2098937/proper-way-to-mock-repository-objects-for-unit-tests-using-moq-and-unity/2102104 – Mark Seemann Apr 11 '10 at 07:04
  • Have you asked your question on the Unity Codeplex site? http://unity.codeplex.com/ You might get an answer there. – Ade Miller Apr 24 '10 at 14:45

1 Answers1

3

I'm not seeing this. I'm using NUnit 2.5.5.10112 and Unity 2.0 (which ships with EntLib, the separate release isn't available just yet).

Update: I just checked with 1.2 and I see your behavior. So this is an issue 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>());
        } 
    }
}

Would it be possible for you to update to Unity 2.0? If not I'll try and dig deeper and find out what's really going on. This may be a limitation of 1.2 though.

Ade Miller
  • 13,575
  • 1
  • 42
  • 75