How to test a generic function with a lambda function as input in Android

Viewed 147

I have a class name ApiCall which is something like a bridge between my ApiService and Repository , the short version of class is :

class ApiCall {
    fun <T> call(req : suspend() -> T) : LiveData<Result<T>>{
        return liveData {
            try {
                emit(Result.success(req.invoke()))
            }catch (e : Exception){
                emit(Result.failure<T>(Exception("Error")))
            }
        }
    }
}

My repository is something like this :

class Repository(val apiCall: ApiCall,var apiService : ApiService) {

    fun getListOfPosts() : LiveData<Result<List<Post>>> = apiCall.call(){
        apiService.getListOfPosts()
    }

}

and ApiService:

interface ApiService {
    @GET("/posts")
    suspend fun getListOfPosts() : List<Post>
}

the problem is I want to make a RepositoryUnitTest class and in this TDD class I want to have a method that checks if apiCall.call() is invoked or not, this is my code:

class RepositoryUnitTest : BaseUnitTest() {

    val apiCall : ApiCall = mock()
    val apiService : ApiService = mock()
    val repository = Repository(apiCall,apiService)

    @Test
    fun `whenever getListOfPosts from repo is called , apiCall must be called too`(){
        repository.getListOfPosts()
        verify(apiCall,times(1)).call<List<Post>>({apiService.getListOfPosts()})
    }

}

but it gives me this error and I don't have any clue what is problem, it talks about differences between two functions but as the error mentions, I don't see any differences!

the error:

_Argument(s) are different! Wanted:
apiCall.call(
(whenever getListOfPosts from repo is called , apiCall must be called too$1) Function1<kotlin.coroutines.Continuation<? super java.util.List<? extends ir.sass.test_sample.Post>>, java.lang.Object>
);
at ir.sass.test_sample.ApiCall.call(ApiCall.kt:8)
Actual invocation has different arguments:
apiCall.call(
    (getListOfPosts$1) Function1<kotlin.coroutines.Continuation<? super java.util.List<? extends ir.sass.test_sample.Post>>, java.lang.Object>
);
at ir.sass.test_sample.Repository.getListOfPosts(Repository.kt:8)_
0 Answers
Related