Android LiveData

2021-05-06  本文已影响0人  wisdom2018

LiveData

LiveData是一种可用于任何数据的封装容器,其中包括可实现Collections的对象,如List。LiveData对象通常存储在ViewModel中,并通过getter方法进行访问

AndroidX Test

AndroidX Test is a collection of libraries for testing. It includes classes and methods that give you versions of components like Applications and Activities, that are meant for tests.

AndroidX test APIS is that they are built to work both for local tests and instrumented tests.

Robolectric

The simulated Android environment that AndroidX Test uses for local tests is provided by Rolbolectic.
Rolbolectic is a library that creates a simulated Android environment for test and runs faster than booting up an emulator or running on a device

@RunWith(AndroidJUnit::class) do?

A test runner is a JUnit component that runs tests. Without a test runner, your tests would not run.@RunWith swaps out that default test runner

The AndroidJunit4 test runner allows for AndroidX Test to run your test differently depending on whether they are instrumented or local tests.


image.png

getContentIfNoteHandled

image.png

summary

  1. Pure view model tests can usually go in the test source set because their code doesn't require Android
  2. you can use the AndroidX test library to get test versions of components like Applications and Activities
  3. If you need to run simulated Android code in your test source set,you can add the Robolectric dependency and the @RunWith(AndroidJUnit4::class)annotation.

https://developer.android.com/codelabs/advanced-android-kotlin-training-testing-basics#13

上一篇下一篇

猜你喜欢

热点阅读