Day82 of #100DaysOfCode
Hii folks 🙌
Today I will be continuing the same pathway in which we will write down the testcase for ViewModel.
Unit 3: Navigation
Pathway 3: Advanced navigation app Examples
https://developer.android.com/courses/android-basics-kotlin/course
Test ViewModels and LiveData
Let’s start with a simple test. The first thing we do when we interact with the app on a device or emulator is to select a quantity of cupcakes. So first we will test the setQuantity()
method in the OrderViewModel
, and check the value of the quantity
LiveData
object.
The quantity
variable, which we are going to test, is an instance of LiveData
. Testing LiveData
objects requires an extra step, and this is where the dependency we added comes into play. We use LiveData
to update our UI as soon as a value changes. Our UI runs on what we call the "main thread." If we are unfamiliar with threading and concurrency, that's okay, we'll go over it in depth in other codelabs. For the time being, in the context of an Android app, think of the main thread as the UI thread. The code that shows the UI to a user runs on this thread. Unless otherwise specified, a unit test assumes that everything runs on the main thread. However, because LiveData
objects cannot access the main thread we have to explicitly state that LiveData
objects should not call the main thread.
- To specify that
LiveData
objects should not call the main thread we need to provide a specific test rule any time we are testing aLiveData
object.
@get:Rule
var instantTaskExecutorRule = InstantTaskExecutorRule()
- Now we can create a function called
quantity_twelve_cupcakes()
. In the method, create an instance of theOrderViewModel.
- In this test, we will be checking to make sure the
quantity
object in theOrderViewModel
is updated whensetQuantity
is called. But before calling any methods or working with any data in theOrderViewModel
, it is important to note that when testing the values of aLiveData
object, the objects need to be observed in order for changes to be emitted. A simple way of doing this is by using theobserveForever
method. Call theobserveForever
method on thequantity
object. This method requires a lambda expression, but that can be left empty. - Then call the
setQuantity()
method, passing in12
as a parameter.
val viewModel = OrderViewModel()
viewModel.quantity.observeForever {}
viewModel.setQuantity(12)
- We can safely infer that the value of the
quantity
object is12
. Note thatLiveData
objects are not the value itself. Values are contained in a property calledvalue
. Make the following assertion:
assertEquals(12, viewModel.quantity.value)
Our test should look like this:
@Test
fun quantity_twelve_cupcakes() {
val viewModel = OrderViewModel()
viewModel.quantity.observeForever {}
viewModel.setQuantity(12)
assertEquals(12, viewModel.quantity.value)
}
One of the main functions of the OrderViewModel
is to calculate the price of our order. This happens when we select a number of cupcakes, and when we select a pickup date. The price calculation happens in a private method, so our test cannot call this method directly. Only other methods in the OrderViewModel
can call it. Those methods are public, so we'll call those in order to trigger the price calculation so we can check that the value of the price is what we expect.
That is all for Day82 ✅
Thanks for reading, See you tomorrow!