Robolectric和谷歌Analytics(分析)的测试误差 [英] Robolectric and Google Analytics Error on Tests

查看:240
本文介绍了Robolectric和谷歌Analytics(分析)的测试误差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在运行与Robolectric测试,这一切都很棒。然后,我实现了谷歌Analytics(分析)到我的应用程序类和测试启动失败。失败的看来,当我在测试过程中膨胀的看法发生。这里的堆栈跟踪:

I've been running tests with Robolectric and it's all been great. Then I implemented Google Analytics to my application class and the tests started failing. The failing seems to occur when I inflate views during tests. Here's the stack trace:

java.lang.NullPointerException: null
at com.google.analytics.tracking.android.AnalyticsGmsCoreClient$AnalyticsServiceConnection.onServiceConnected(AnalyticsGmsCoreClient.java:176)
at org.robolectric.shadows.ShadowApplication$2.run(ShadowApplication.java:209)
at org.robolectric.util.Scheduler$PostedRunnable.run(Scheduler.java:162)
at org.robolectric.util.Scheduler.runOneTask(Scheduler.java:107)
at org.robolectric.util.Scheduler.advanceTo(Scheduler.java:92)
at org.robolectric.util.Scheduler.advanceToLastPostedRunnable(Scheduler.java:68)
at org.robolectric.util.Scheduler.unPause(Scheduler.java:25)
at org.robolectric.shadows.ShadowLooper.unPause(ShadowLooper.java:219)
at org.robolectric.shadows.ShadowLooper.runPaused(ShadowLooper.java:258)
at org.robolectric.shadows.ShadowViewGroup.addView(ShadowViewGroup.java:32)
at android.view.ViewGroup.addView(ViewGroup.java)
at android.view.ViewGroup.addView(ViewGroup.java:3225)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:750)
at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
at org.robolectric.shadows.ShadowView.inflate(ShadowView.java:82)
at android.view.View.inflate(View.java)

这里的Robolectric code我BaseActivity:

Here's the Robolectric code for my BaseActivity:

@RunWith(RobolectricTestRunner.class)
public class BaseActivityTest {

ActivityController<TestActivity> activityController;
TestActivity activity;

@Before
public void setUp(){
    activityController = Robolectric.buildActivity(TestActivity.class).create().start();
}


@After
public void takeDown(){
    activityController.stop().destroy();
    activity = null;
}

@Test
public void testOnPauseState(){
    activity = activityController.resume().pause().get();
    assertFalse(activity.getBus().isActive());
}
}

继<一href="https://developers.google.com/analytics/devguides/collection/android/v3/advanced#example">Google分析例如,我的应用程序类实现谷歌Analytics(分析)。每当我跑的测试,我的应用程序,在发生断裂。我已经试过落实Robolectric的MockApplication对象,但是这并没有改变;我不得不从我的申请对象中删除谷歌Analytics(分析)有它的工作。没有人有任何解决方案,可以让我与谷歌Analytics(分析)运行Robolectric?

Following the Google Analytics example, my Application class implements Google Analytics. Whenever I run the tests with my Application, the break occurs. I've tried implementing Robolectric's MockApplication object, but that had no change; I had to remove Google Analytics from my Application object to have it work. Does anyone have any solutions that would allow me to run Robolectric with Google Analytics?

推荐答案

我有两个建议你,如果你使用的是谷歌Analytics(分析)V3:

I have two suggestions for you if you are using Google Analytics V3:

1)你有没有打过电话,当你运行你的测试这种方法?

1) Have you tried calling this method when you run your tests?

 googleAnalytics.setAppOptOut(true); 

这将禁用谷歌分析的测试的寿命。

This will disable google analytics for the lifetime of the test.

2)创建在同一封装测试应用程序类作为当前应用程序并将其命名为相同的名称,但prePEND测试的名称。此应用程序将在您的测试创建,你可以设置setAppOptOut设置为true或者你可以从它完全忽略GoogleAnalytics。

2) Create a test application class in the same package as your current application and name it the same name but prepend "Test" to the name. This application will be created during your tests and you can set "setAppOptOut" set to true or you can omit GoogleAnalytics altogether from it.

更多信息可以在这里找到: HTTP:/ /robolectric.blogspot.com/2013/04/the-test-lifecycle-in-20.html

More information can be found here: http://robolectric.blogspot.com/2013/04/the-test-lifecycle-in-20.html

这篇关于Robolectric和谷歌Analytics(分析)的测试误差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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