C# 如何获取测试VideoViewModel的上下文(Xamarin,NUnitLite)
我想在android上测试抽象VideoViewModel的具体实现。我将VS2013与NUnitLite一起使用 为了创建VideoViewModel实例,我需要活动上下文 以下是VideoViewModel的构造函数:C# 如何获取测试VideoViewModel的上下文(Xamarin,NUnitLite),c#,xamarin,xamarin.android,nunit,C#,Xamarin,Xamarin.android,Nunit,我想在android上测试抽象VideoViewModel的具体实现。我将VS2013与NUnitLite一起使用 为了创建VideoViewModel实例,我需要活动上下文 以下是VideoViewModel的构造函数: public VideoViewModelAndroid(Context context, IFileSystem fileSystem) : base(fileSystem) { bool contextIsActivity
public VideoViewModelAndroid(Context context, IFileSystem fileSystem)
: base(fileSystem)
{
bool contextIsActivity = context is Activity;
ExceptionUtil.ThrowIf.IsFalse(() => contextIsActivity);
this.context = context;
this.videoView = (context as Activity).FindViewById<VideoView>(Resource.Id.videoView1); // Returns null for the videoView
this.videoView.Touch += videoView_Touch;
上下文来自这里:
[Activity(Label = "IDS.Droid.Tests", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : TestSuiteActivity
{
public static Context con;
protected override void OnCreate(Bundle bundle)
{
// tests can be inside the main assembly
AddTest(Assembly.GetExecutingAssembly());
// or in any reference assemblies
// AddTest (typeof (Your.Library.TestClass).Assembly);
// Once you called base.OnCreate(), you cannot add more assemblies.
base.OnCreate(bundle);
//SetContentView(Resource.Layout.MainLayout);
con = this;
}
}
我从应用程序链接了布局,id在Resource.Designer.cs中表示
但它调用后不返回视图
this.videoView = (context as Activity).FindViewById<VideoView>(Resource.Id.videoView1);
this.videoView=(作为活动的上下文).findviewbyd(Resource.Id.videoView1);
有什么办法解决这个问题吗?你真的需要上下文吗,或者你可以模仿它?如果可以模拟它,请使用此包(可通过nuget获得): github: 努吉:
但正如其他人在评论中所说,在viewModel中使用上下文并不是一个好的实践。原因是ViewModel必须独立于平台。如果您想在iOS和WinPhone上重复使用viewModel,您可能会遇到这些平台上不存在的上下文问题。您在哪里使用VideoView来扩展布局?通常,您会使用SetContentView(),但您已经对此进行了注释。是的,在一次测试中,我尝试扩大布局(在调用新的VideoViewModelAndroid(上下文,文件系统)之前),但这没有帮助…:-(视图模型与视图具有强绑定?现在这非常少见…Moq与Xamarin不兼容。Android…:-(我们每个平台都有一个抽象的ViewModel,但是它们都是抽象的,我不能测试抽象类。我想测试视图的具体实现。它可以与wpf配合使用,但不能与Android配合使用……:-(好的,我明白了……不过你还是通过为每个平台创建ViewModel来打破MVVM模式。你的ViewModel应该是独立于平台的,不应该包含任何逻辑。它们用于使用项目其他部分编写的逻辑加载数据,并通过属性将其呈现给页面/活动/ViewController。在你的情况下,你应该考虑关于修改ViewModel。ViewModel没有很多方法。它们是播放、停止、显示…但它们内部有一些自定义逻辑,如visiblestate和从文件系统加载视频…它们是平台特定的功能,不能独立于平台。。。
this.videoView = (context as Activity).FindViewById<VideoView>(Resource.Id.videoView1);