【问题标题】:How do I write Firebase Android Instrumentation Tests?如何编写 Firebase Android Instrumentation 测试?
【发布时间】:2016-03-15 15:00:03
【问题描述】:

我正在尝试使用 Firebase 作为我的应用的后端。如果我在我的应用程序中使用以下 quickstart guide 一切正常。

如果我检查我的 firebase 数据页面,则数据已成功写入。

但是,如果我尝试在 androidTest(仪器测试)中做同样的事情,什么也不会发生 - 没有数据写入 firebase 数据库。我在我的 androidTest 清单中指定了 Internet 权限,所以想知道我是否需要做其他事情才能从我的测试中写入 firebase?

在相关说明中,一旦我可以在仪器测试中做到这一点,有没有办法可以在单元测试中做到这一点?

非常感谢,

里兹

编辑:这是我要运行的测试:

public class FirebaseTest extends InstrumentationTestCase{
    private static final String FIREBASE = "https://my-app-name.firebaseio.com/";

    @Override
    @Before
    public void setUp() {
        injectInstrumentation(InstrumentationRegistry.getInstrumentation());
        Firebase.setAndroidContext(getInstrumentation().getTargetContext());
    }

   @Test
   public void testWrite(){
       Firebase cloud = new Firebase(FIREBASE);
       cloud.child("message").setValue("Do you have data? You'll love Firebase.");
   }
}

【问题讨论】:

  • 我遇到了同样的问题,但为了清楚起见,您可以编辑您的问题以包含测试示例吗?
  • 我已编辑问题以包含我要运行的测试。
  • 您的类应该扩展 Application 或 Activity 以使其工作,或者您可以做的是创建一个应用程序类并在 oncreate 上进行初始设置为Firebase.setAndroidContext(this);,然后进一步完成您的工作
  • @1shubhamjoshi1:我认为扩展活动并不重要。在我的应用程序中,我将 firebase 放在了一个没有引用活动的辅助类中。我将上下文传递给助手类,它工作正常。在上面的仪器测试中,我通过了应该是同一件事的目标上下文,

标签: android firebase


【解决方案1】:

在您的主应用程序中,您可以定义一个 androidApplication 类来初始化您的 Firebase 上下文。然后创建一个扩展 ApplicationTestCase 的 ApplicationTest :

在主要来源:

public class MyApplication extends android.app.Application {

    @Override
    public void onCreate() {
       super.onCreate();
       Firebase.setAndroidContext(this); //initializeFireBase(context);
       isInitialized = true;
    }
}

在您的 Android 测试中:

public class ApplicationTest extends ApplicationTestCase<MyApplication> {

    private static MyApplication application;

    public ApplicationTest() {
        super(MyApplication.class);
    }

   @Override
   public void setUp() throws Exception {
      super.setUp();
      if (application == null) {
          application = getApplication();
      }
      if (application == null) {
         application = (MyApplication) getContext().getApplicationContext();
         assertNotNull(application);
         long start = System.currentTimeMillis();
         while (!application.isInitialized()){
             Thread.sleep(300);  //wait until FireBase is totally initialized
             if ( (System.currentTimeMillis() - start ) >= 1000 )
                 throw new TimeoutException(this.getClass().getName() +"Setup timeOut");
         }
      }
   }


   @Test
   public void testWrite(){
       Firebase cloud = new Firebase(FIREBASE);
       cloud.child("message").setValue("Do you have data? You'll love Firebase.");
   }

}

【讨论】:

  • 谢谢,我已经尝试过了,但不起作用。测试无法启动,因为它无法通过 createApplication() 创建 MyApplication(顺便说一句,我认为应该有一个 super.setup(); createApplication(); 在每次测试之前运行的 setUp 方法中)。它在 Instrumentation.newApplication(mApplicationClass, getContext()) 上失败;在 ApplicationTestCase.java...
  • 为了避免在“onCreate()”方法中多次传递,我在静态模式下使用商店“MyApplication”做了一个丑陋的设置。我会编辑我的答案给你看
  • 感谢您的回复。不幸的是,尝试运行上述代码时,我在以下行遇到异常:application = (MyApplication) getContext().getApplicationContext();。我认为这是 JUnitRunner 某处的 NullPointerException(在 ReflectiveCallable.java 中)。
  • 你确定你的测试类扩展了 android.test.ApplicationTestCase 吗?如果您使用 AndroidStudio,请不要忘记在 Build Variant 选项卡中选择 Android Instrumentation Tests !!
  • 什么是 InitializeFirebase?那是你要调用 Firebase.setAndroidContext 的地方吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多