我正在尝试使用mvvmcross为使用xamaring编写的android / ios应用程序编写单元测试。我已按照wiki中的说明操作了,当服务尝试以这种方式更改ViewModel时,它们确实运行良好:
var viewDispatcher = Mvx.Resolve<IMvxViewDispatcher>();
viewDispatcher?.ShowViewModel(
new MvxViewModelRequest(typeof(HomeViewModel), null, null, MvxRequestedBy.Unknown));
测试在Mvx.Resolve();
的第一行失败。我假设这是注册模拟IoC容器中的接口:
this.mockDispatcher = new MockDispatcher();
this.Ioc.RegisterSingleton<IMvxViewDispatcher>(this.mockDispatcher);
this.Ioc.RegisterSingleton<IMvxMainThreadDispatcher(this.mockDispatcher);
所以当这种方式调用时,Mvx无法解析。可以测试此代码还是有其他可能从服务中更改ViewModel?
答案 0 :(得分:4)
我认为你的AdditionalSetup
永远不会被召唤。如果使用nunit,则必须将SetUp
属性添加到设置方法并调用Setup()
MvxIoCSupportingTest
,否则调用相应的属性。
public abstract class MvxTestBase : MvxIoCSupportingTest
{
protected MockDispatcher mockDispatcher;
protected override void AdditionalSetup()
{
this.mockDispatcher = new MockDispatcher();
this.Ioc.RegisterSingleton<IMvxViewDispatcher>(this.mockDispatcher);
this.Ioc.RegisterSingleton<IMvxMainThreadDispatcher>(this.mockDispatcher);
}
[SetUp]
public virtual void SetupTest()
{
Setup();
}
}
或者您在每个测试中调用它,如下所示:https://mvvmcross.com/docs/testing#section-test-class-declaration-and-setup