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.
No comments:
Post a Comment