【发布时间】:2018-10-18 16:04:17
【问题描述】:
- Prism.Ninject 7.1.0.431
- 棱镜 7.1.0.431
- NUnit 3.3.3
- N替换
在使用 Prism 6.3 之前,我们进行了一组单元测试,以确认我们的所有绑定都已到位,如下所示
protected IKernel TestKernel;
[SetUp]
public void Given
{
TestKernel = new StandardKernel();
SUT = new MyModule( TestKernel );
Core = Assembly.Load( "MyDLL.Core" ).GetTypes();
Common = Assembly.Load( "MyDLL.Common" ).GetTypes();
SUT.Initialize();
}
[ Test ]
public void Then_ViewModels_Will_Be_Bound()
{
var testCollection = Common
.Where( item => item.IsPublic )
.Where( item => item.Name.EndsWith( "ViewModel" ) );
foreach ( var item in testCollection )
{
Assert.That( TestKernel.GetBindings( item ).Any, $"Test Failed: {item.Name}" );
}
}
但是在 Ninject 7.1 中,IModule 接口发生了一些变化,所以现在部件的注册方式有所不同
public void RegisterTypes(
IContainerRegistry containerRegistry )
我只是想用这种新的 IModule 格式重新启动和运行我的单元测试。我曾尝试将我的给定更改为如下
protected override void Given()
{
TestKernel = new StandardKernel();
TestContainerRegistry = Substitute.For<IContainerRegistry>();
TestContainerRegistry.GetContainer().Returns( TestKernel );
SUT = new MyModule();
}
但是,当我尝试运行测试时,我得到了以下结果。
System.InvalidCastException:无法将“Castle.Proxies.IContainerRegistryProxy”类型的对象转换为“Prism.Ioc.IContainerExtension`1[Ninject.IKernel]”类型。
如果有人知道我可以如何模拟它,我将不胜感激,因为我目前处于僵局。
【问题讨论】:
-
为什么不能使用真正的实现?您不会为 Prism 的其他组件编写测试,是吗?所以你相信它们能正常工作,就像你相信 NSubstitute 一样,它们应该可以在测试中使用。尤其是在这种情况下,我们谈论的是薄包装。