-
Notifications
You must be signed in to change notification settings - Fork 8
PartTests
Arnaud Giuliani edited this page Sep 27, 2018
·
7 revisions
- 1.1 - First ViewModel & LiveData with Weather Detail View
- 1.2 - ViewModel & Events with the Splash View
- 1.3 - Shared ViewModel between Activity/Fragments, with the Weather View
Let's update the corresponding unit tests.
- Let's create the
DetailViewModelMockTest
fromDetailPresenterMockTest
- Your mocked view is now an Android Observer (
android.arch.lifecycle.Observer
) of State:
@Mock
lateinit var view: Observer<ViewModelState>
- Rename your
presenter
property toviewModel
and change its type toDetailViewModel
Let's update the before()
function to setup your dependency:
- Also make the
view
observe the ViewModel withobserveForever
function:viewModel.states.observeForever(view)
- Uncomment the
InstantTaskExecutorRule
rule to allow LiveData execution in test
Now update the tests to check that view
has been notified with an ArgumentCaptor
:
// setup ArgumentCaptor
val arg = ArgumentCaptor.forClass(ViewModelState::class.java)
// Here we expect 2 calls on view.onChanged
verify(view, times(2)).onChanged(arg.capture())
val values = arg.allValues
// Test obtained values in order
assertEquals(2, values.size)
assertEquals(Loading, values[0])
assertEquals(DetailViewModel.DetailLoaded(weather), values[1])
Update the corresponding tests in SplashViewModelMockTest.kt
:
- Create
SplashViewModelMockTest
fromSplashPresenterMockTest
class - Uncomment
InstantTaskExecutorRule
- Rename
presenter
property toviewModel
- Change type of
view
property toObserver<ViewModelEvent>
- Update
before()
function to observe your viewModel - Update the tests using
ArgumentCaptor
to check the returned events
Let's update our tests. From the WeatherHeaderPresenterMockTest
class:
- Delete
WeatherListPresenterMockTest
- Rename
WeatherHeaderPresenterMockTest
class toWeatherViewModelMockTest
- Rename
presenter
property toviewModel
- Change type of
view
property toObserver<ViewModelState>
, and rename itstatesView
- Add a new view,
eventsView
of typeObserver<ViewModelEvent>
- Uncomment
InstantTaskExecutorRule
- Update
before()
function to observe withstatesView
andeventsView
- Update the tests using
ArgumentCaptor
to check the returned states and events (look at theMockedData
object which contains mocked data)