嗨,我很难模拟 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。