测试活动中Xamarin.Android [英] Testing Activities in Xamarin.Android

查看:194
本文介绍了测试活动中Xamarin.Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想单元测试上的活动生命周期的某些相互作用(我工作的一个数据绑定库,不能有单元测试没有它)。

I'm wanting to unit test some interactions on activity lifecycle (I'm working on a data binding library, that can't have unit tests without it).

Android有一些关于这个问题很好文档。但是,我还没有发现任何人试图与Xamarin做到这一点。许多的在机器人。测试中缺少Xamarin。

Android has some good documentation on the subject. However, I've not found anyone that has attempted to do this with Xamarin. Many of the classes in android.test are missing in Xamarin.

到目前为止,我试图像在使用NUnit的的Xamarin测试项目如下:

So far I've attempted something like the following in the Xamarin test project that uses NUnit:

    [TestFixture]
    public class MyActivityTests
    {
        private TestActivity _activity;
        private TestViewModel _viewModel;
        private Instrumentation _instrumentation;

        [SetUp]
        public void SetUp()
        {
            _viewModel = new TestViewModel();
            _activity = new TestActivity();

            _instrumentation = new InstrumentationTestRunner();
            _instrumentation.Start();
            _instrumentation.CallActivityOnCreate(_activity, Bundle.Empty);
        }
    }

后来我不得不使用的活性测试。

Later on I'd have tests that use the activity.

我也有设置在我的清单如下:

I also have to set the following in my manifest:

<application android:label="My Tests">
    <uses-library android:name="android.test.runner" />
</application>

问题是,在上面我的单元测试显示java.lang.NullPointerException 中抛出我的活动的 base.OnCreate()方法。我敢肯定,我失去了一些东西,因为我在未知的水域在这里。

The problem is, in my unit test above a Java.Lang.NullPointerException is thrown in my activity's base.OnCreate() method. I'm sure I'm missing something, because I'm in uncharted waters here.

下面是堆栈跟踪:

[MonoDroid] UNHANDLED EXCEPTION: Java.Lang.NullPointerException: Exception of type 'Java.Lang.NullPointerException' was thrown.
[MonoDroid] at Android.Runtime.JNIEnv.CallNonvirtualVoidMethod (intptr,intptr,intptr,Android.Runtime.JValue[]) [0x00084] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.10.2-branch/35376881/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:895
[MonoDroid] at Android.App.Activity.OnCreate (Android.OS.Bundle) [0x00070] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.10.2-branch/35376881/source/monodroid/src/Mono.Android/platforms/android-18/src/generated/Android.App.Activity.cs:2193
[MonoDroid] at XamBind.Droid.Tests.TestActivity.OnCreate (Android.OS.Bundle) [0x00003] in /Users/jonathanpeppers/Desktop/MonoTouch/XamBind/XamBind.Droid.Tests/Activities/TestActivity.cs:19
[MonoDroid] at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ (intptr,intptr,intptr) [0x00011] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.10.2-branch/35376881/source/monodroid/src/Mono.Android/platforms/android-18/src/generated/Android.App.Activity.cs:2178
[MonoDroid] at (wrapper dynamic-method) object.640629b6-d042-45f3-840b-28e31832a912 (intptr,intptr,intptr) <IL 0x00017, 0x0001f>
[MonoDroid] 
[MonoDroid]   --- End of managed exception stack trace ---
[MonoDroid] java.lang.NullPointerException
[MonoDroid]     at android.app.Activity.onCreate(Activity.java:884)
[MonoDroid]     at xambind.droid.tests.TestActivity.n_onCreate(Native Method)
[MonoDroid]     at xambind.droid.tests.TestActivity.onCreate(TestActivity.java:28)
[MonoDroid]     at android.app.Activity.performCreate(Activity.java:5133)
[MonoDroid]     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
[MonoDroid]     at mono.android.view.View_OnClickListenerImplementor.n_onClick(Native Method)
[MonoDroid]     at mono.android.view.View_OnClickListenerImplementor.onClick(View_OnClickListenerImplementor.java:29)
[MonoDroid]     at android.view.View.performClick(View.java:4240)
[MonoDroid]     at android.view.View$PerformClick.run(View.java:17721)
[MonoDroid]     at android.os.Handler.handleCallback(Handler.java:730)
[MonoDroid]     at android.os.Handler.dispatchMessage(Handler.java:92)
[MonoDroid]     at android.os.Looper.loop(Looper.java:137)
[MonoDroid]     at android.app.ActivityThread.main(ActivityThread.java:5103)
[MonoDroid]     at java.lang.reflect.Method.invokeNative(Native Method)
[MonoDroid]     at java.lang.reflect.Method.invoke(Method.java:525)
[MonoDroid]     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
[MonoDroid]     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
[MonoDroid]     at dalvik.system.NativeStart.main(Native Method)

有没有人尝试过​​呢?

Has anyone attempted this before?

推荐答案

我想,这是不是与Xamarin.Android被支持:的 https://bugzilla.xamarin.com/show_bug.cgi?id=6043

I think this isn't supported yet with Xamarin.Android: https://bugzilla.xamarin.com/show_bug.cgi?id=6043

由于这个东西取决于JUnit的,他们不能绑定的类C#。希望他们得到它的工作和它的未来。

Because this stuff depends on JUnit, they can't bind the classes to C#. Hopefully they get it working and its coming.

我所做的我的情况是重写我的库,因此你不必继承活动,那么我可以写我的测试没有问题的。

What I did for my situation was to rewrite my library so you don't have to subclass Activity, then I could write my tests no problem.

这篇关于测试活动中Xamarin.Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆