Skip to content

Instantly share code, notes, and snippets.

// given
// api returns contacs list
// when
// fetch contacts is initiated
// then
// assert progress was shown
// assert set contacts list was called
// assert progress was hidden
view.hideProgress()
when(it){
is NoConnectionException -> view.showNoConnectionError()
is ApiException -> view.showError(it.msg)
}
data class Contact(val name:String, val phoneNum:String)
interface ContactsListsApi{
fetchContacts(): Single<List<Contact>>
}
interface ContactsListsContract{
interface View{
fun showProgress()
fun hideProgress()
fun setData(contacts: List<Contact>)
//given
// api returns error
//when
// fetching contacts list
//then
// show error message
data class Contact(val name:String, val phoneNum:String)
interface ContactsListsApi{
fetchContacts(): Single<List<Contact>>
}
interface ContactsListsContract{
interface View{
fun showProgress()
fun hideProgress()
fun setData(contacts: List<Contact>)
data class Contact(val name:String, val phoneNum:String)
interface ContactsListsApi{
fetchContacts(): Single<List<Contact>>
}
interface ContactsListsContract{
interface View{
fun showProgress()
fun hideProgress()
fun setData(contacts: List<Contact>)
whenever(repository.fetchContacts()).thenReturn(emptyList()) //simulate interface response
verify(view).setData(any()) //verify `setData` method was called
verify(view, never()).setData(any()) //verify `setData` was never called
verify(view, times(2)).setData(any()) //verify `setData` was called exactly two times
...
//verify that `showProgress` and `getCurrentUser` were called in that exact order
val inOrder = inOrder(view,repository)
inOrder.verify(view).showProgress()
inOrder.verify(repository).getCurrentUser()
@Test
fun `fetch contacts, api error`(){
//given
val errorMessage = "Bad request"
whenver(api.fetchContacts()).thenReturn{Single.error(IllegalArgumentException(errorMessage)}
//when
presenter.fetchContacts()
//then
val inOrder = inOrder(view, api)
inOrder.verify(view).showProgress()
data class Contact(val name:String, val phoneNum:String)
interface ContactsListsApi{
fetchContacts(): Single<List<Contact>>
}
interface ContactsListsContract{
interface View{
fun showProgress()
fun hideProgress()
fun setData(contacts: List<Contact>)
interface MyLocationProvider{
fun obtainCurrentLocation():LatLng
}
class SomePresenter(val locationProvider: MyLoacationProvider){
...
}