Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在android单元测试中模拟挂起的函数调用_Android_Unit Testing_Mockito_Mockito Kotlin - Fatal编程技术网

在android单元测试中模拟挂起的函数调用

在android单元测试中模拟挂起的函数调用,android,unit-testing,mockito,mockito-kotlin,Android,Unit Testing,Mockito,Mockito Kotlin,我想测试一个名为withCredentials的函数,该函数用于传递使用用户凭据进行API调用所需的参数,如下所示: @VisibleForTesting(otherwise = VisibleForTesting.PRIVATE) internal suspend fun <T> withCredentials(block: suspend (String) -> Response<T>): ServiceResponse<T> val respo

我想测试一个名为
withCredentials
的函数,该函数用于传递使用用户凭据进行API调用所需的参数,如下所示:

@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
internal suspend fun <T> withCredentials(block: suspend (String) -> Response<T>): ServiceResponse<T> 
val response = withCredentials{ bearer ->  service.logIn(bearer, userName, password) }
fun `test with credentials is calls nothing when there are no credentials saved`() {
    runBlocking {
        whenever(prefs.auth).thenReturn(null)
        
        val response = withCredentials(this@CoreTests::testSuccess)

        assertEquals(0, called)

    }
}


private var called = 0
private suspend fun testSuccess(bearer: String?): Response<String> {
        delay(1)
        called++
        return Response.success(200, "$bearer test")
    }
private val functionCall = mock(suspend (String?)-> ServiceResponse<String>) //this is ofc incorrect I need the correct code here

fun `test with credentials is calls nothing when there are no credentials saved`() {
    runBlocking {
        whenever(prefs.auth).thenReturn(null)
        
        val response = withCredentials(this@CoreTests::testSuccess)

        verify(functionCall,never()).invoke(any())

    }
}   
我希望测试withCredentials方法,而不需要测试特定的场景(例如,在登录过程中调用上述方法)

我现在做的是这样的:

@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
internal suspend fun <T> withCredentials(block: suspend (String) -> Response<T>): ServiceResponse<T> 
val response = withCredentials{ bearer ->  service.logIn(bearer, userName, password) }
fun `test with credentials is calls nothing when there are no credentials saved`() {
    runBlocking {
        whenever(prefs.auth).thenReturn(null)
        
        val response = withCredentials(this@CoreTests::testSuccess)

        assertEquals(0, called)

    }
}


private var called = 0
private suspend fun testSuccess(bearer: String?): Response<String> {
        delay(1)
        called++
        return Response.success(200, "$bearer test")
    }
private val functionCall = mock(suspend (String?)-> ServiceResponse<String>) //this is ofc incorrect I need the correct code here

fun `test with credentials is calls nothing when there are no credentials saved`() {
    runBlocking {
        whenever(prefs.auth).thenReturn(null)
        
        val response = withCredentials(this@CoreTests::testSuccess)

        verify(functionCall,never()).invoke(any())

    }
}   
fun`testwithcredentials是在没有保存凭据时调用nothing`(){
运行阻塞{
无论何时(prefs.auth).thenReturn(null)
val响应=withCredentials(this@CoreTests::testSuccess)
assertEquals(0,已调用)
}
}
调用的私有变量=0
private-suspend-fun-testSuccess(承载者:字符串?):响应{
延迟(1)
叫++
返回响应。成功(200,“$承载测试”)
}
为了验证(在本例中)在没有保存凭据时从未调用testSuccess

有没有办法模拟suspend函数,这样我就可以执行以下操作:

@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
internal suspend fun <T> withCredentials(block: suspend (String) -> Response<T>): ServiceResponse<T> 
val response = withCredentials{ bearer ->  service.logIn(bearer, userName, password) }
fun `test with credentials is calls nothing when there are no credentials saved`() {
    runBlocking {
        whenever(prefs.auth).thenReturn(null)
        
        val response = withCredentials(this@CoreTests::testSuccess)

        assertEquals(0, called)

    }
}


private var called = 0
private suspend fun testSuccess(bearer: String?): Response<String> {
        delay(1)
        called++
        return Response.success(200, "$bearer test")
    }
private val functionCall = mock(suspend (String?)-> ServiceResponse<String>) //this is ofc incorrect I need the correct code here

fun `test with credentials is calls nothing when there are no credentials saved`() {
    runBlocking {
        whenever(prefs.auth).thenReturn(null)
        
        val response = withCredentials(this@CoreTests::testSuccess)

        verify(functionCall,never()).invoke(any())

    }
}   
private val functionCall=mock(挂起(字符串?->servicesponse)//这是ofc不正确的我需要正确的代码
fun`testwithcredentials是在没有保存凭据时调用nothing`(){
运行阻塞{
无论何时(prefs.auth).thenReturn(null)
val响应=withCredentials(this@CoreTests::testSuccess)
验证(functionCall,never())。调用(any())
}
}