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:
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.