Reputation: 4388
I am trying to use MockK in my project,
when I try to verify an api call in the interface I get an error
Suspension functions can be called only within coroutine body
this is my code
import com.example.breakingbad.api.ApiServiceInterface
import com.example.breakingbad.data.DataRepository
import io.mockk.impl.annotations.InjectMockKs
import io.mockk.impl.annotations.MockK
import io.mockk.verify
import kotlinx.coroutines.test.runBlockingTest
import org.junit.Test
class DataRepositoryTest {
@MockK
private lateinit var apiServiceInterface: ApiServiceInterface
@InjectMockKs
private lateinit var dataRepository: DataRepository
@Test
fun getCharacters() {
runBlockingTest {
val respose = dataRepository.getCharacters()
verify { apiServiceInterface.getDataFromApi() } // HERE IS THE ERROR
}
}
}
DataRepository
class DataRepository @Inject constructor(
private val apiServiceInterface: ApiServiceInterface
) {
suspend fun getCharacters(): Result<ArrayList<Character>> = kotlin.runCatching{
apiServiceInterface.getDataFromApi()
}
}
interface
interface ApiServiceInterface {
@GET("api/characters")
suspend fun getDataFromApi(): ArrayList<Character>
}
what am I doing wrong here?
Upvotes: 5
Views: 3675
Reputation: 93639
verify
takes a standard function as an argument. You need to use coVerify
which takes a suspend function as it’s argument.
Upvotes: 18