简体   繁体   中英

Mocking UnityContainer RegisterType results in System not supported exception

Hi I am having a hard time mocking IUnityContainer , specificaly I am trying to see if Register Type was called.This is the method I am trying to test:

    private readonly IUnityContainer _container;

    public InjectorContainer(IUnityContainer container)
    {
        _container = container;
    }

    public void RegisterType(InjectorServiceModel dependencyService)
    {
        _container.RegisterType(dependencyService.From, dependencyService.To);
    }

This is my Unity test class:

 private Mock<IUnityContainer> _unitContaineMock;
    private InjectorContainer _injectorContainer;

    [TestInitialize]
    public void Initializer()
    {
        _unitContaineMock = new Mock<IUnityContainer>();
        _injectorContainer = new InjectorContainer(_unitContaineMock.Object);
    }

    [TestMethod]
    public void RegisterType_CheckIfContainerRegisterTypeIsCalled_Oance()
    {
        //Arrange
        var injectorServiceModel = new InjectorServiceModel()
        {
            From = typeof(IInjectorContainerFake),
            To = typeof(InjectorContainerFake)
        };
        bool wasCalled = false;
        _unitContaineMock.Setup(x => x.RegisterType(It.IsAny<Type>(), It.IsAny<Type>())).Callback(() =>
        {
            wasCalled = true;
        });
        //Act
        _injectorContainer.RegisterType(injectorServiceModel);

        //Assert
        Assert.IsTrue(wasCalled);
    }

The code in this state is actual my second attemt I first tryed doing it like this:

 [TestMethod]
    public void RegisterType_CheckIfContainerRegisterTypeIsCalled_Oance()
    {
        //Arrange
        var injectorServiceModel = new InjectorServiceModel()
        {
            From = typeof(IInjectorContainerFake),
            To = typeof(InjectorContainerFake)
        };
        //Act
        _injectorContainer.RegisterType(injectorServiceModel);

        //Assert
        _unitContaineMock.Verify(x => x.RegisterType(It.IsAny<Type>(), It.IsAny<Type>()), Times.Once);

    }

In both case I get a SystemNotSuported exception that has this message:

Invalid verify on a non-virtual (overridable in VB) member: x => x.RegisterType(It.IsAny(), It.IsAny(), new[] { })

From what I could understand from this it seems that when it is trying to veryfying it is looking for a RegisterType with 3 paramaters.

Does anyone know what am I doing wrong here?

I am trying to test if RegisterType has been called.

I'm 99% sure the reason you're getting this is because the RegisterType overload you're calling is actually an extension method defined elsewhere, and you can't use Setup/Verify on extension methods (because they're technically static). I base this on the method list for IUnityContainer here: http://msdn.microsoft.com/en-us/library/microsoft.practices.unity.iunitycontainer_members(v=pandp.30).aspx . The only RegisterType that's actually defined on the interface is this one, which as you can see takes five parameters.

Possible solutions:

  1. Use the overload that takes five parameters instead. You'll be able to do a Verify on that one. But it's of course rather silly to make your code more complex just so you'll be able to write a unit test.
  2. Use a "real" container instead of a mock, and instead of doing a Verify, call one of the IsRegistered overloads in your unit test to check if the type has actually been added to the container. This might be a preferable approach anyway. It will certainly be less brittle. Your unit test shouldn't really care which particular method was used to add the types to the container; it should just verify that the types have been added.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM