[英]Mocking UnityContainer RegisterType results in System not supported exception
嗨,我在模拟IUnityContainer时遇到了麻烦,特别是我想查看是否调用了Register Type。这是我要测试的方法:
private readonly IUnityContainer _container;
public InjectorContainer(IUnityContainer container)
{
_container = container;
}
public void RegisterType(InjectorServiceModel dependencyService)
{
_container.RegisterType(dependencyService.From, dependencyService.To);
}
这是我的Unity测试课程:
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);
}
这种状态下的代码是我实际尝试过的第二次尝试,实际上是这样的:
[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);
}
在这两种情况下,我都会收到一个具有以下消息的SystemNotSuported异常:
对非虚拟成员(在VB中可重写)的无效验证:x => x.RegisterType(It.IsAny(),It.IsAny(),new [] {})
从我的理解中可以看出,它似乎在寻找具有3个参数的RegisterType时。
有人知道我在这里做错什么吗?
我正在尝试测试是否已调用RegisterType。
我99%确信得到这个的原因是因为您要调用的RegisterType
重载实际上是在其他地方定义的扩展方法,并且您不能在扩展方法上使用Setup / Verify(因为它们在技术上是静态的) 。 我基于IUnityContainer
的方法列表在这里: http : //msdn.microsoft.com/en-us/library/microsoft.practices.unity.iunitycontainer_members ( IUnityContainer
.aspx 。 接口上实际定义的唯一RegisterType
就是这个,您可以看到它带有五个参数。
可能的解决方案:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.