0

测试方法

@Test
fun firebaseRecordException_whenCallReportApiErrorNonFatal_shouldCallOnlyOnce() {
    val exception = Exception("Exception Message: Backend API error")
    doNothing().
    `when`(firebaseCrashlytics).setCustomKey(eq("Exception Message: "), eq("Backend API error"))
    doNothing().
    `when`(firebaseCrashlytics).recordException(eq(exception))

    firebaseLogServiceImplementation.reportApiErrorNonFatal(
        "Exception Message: Backend API error",
        LogError.BackendAPIError,
        null,
        null
    )

    // Verify
    verify(firebaseCrashlytics, times(1)).recordException(eq(exception))
}

使用此测试方法,我需要验证该firebaseCrashlytics.recordException(exception)方法是否调用过一次。

方法

override fun reportApiErrorNonFatal(exceptionCause: String, error: LogError, body: String ? , requestId : String ? ) {
    val exception = Exception(exceptionCause)

    firebaseCrashlytics.setCustomKey(EXCEPTION_MESSAGE, error.getErrorMessage())
    // The request id will be helpful to investigate the exact error
    requestId?.let {
        FirebaseCrashlytics.getInstance().setCustomKey(REQUEST_ID, it)
    }
    // The full body will be helpful to investigate the exact error
    body?.let {
        FirebaseCrashlytics.getInstance().setCustomKey(EXCEPTION_BODY, it)
    }

    firebaseCrashlytics.recordException(exception)
}

这是我用来向 firebase 报告非致命异常的方法。

测试用例的错误日志

Argument(s) are different!Wanted:
    firebaseCrashlytics.recordException(
        java.lang.Exception: Exception Message: Backend API error
    ); 
-> at com.google.firebase.crashlytics.FirebaseCrashlytics.recordException(FirebaseCrashlytics.java: 284)
Actual invocations have different arguments:
    firebaseCrashlytics.setCustomKey(
        "Exception Message: ",
        "Backend API error"
    ); 
-> at jp.co.rakuten.pointclub.android.services.log.FirebaseLogServiceImplementation.reportApiErrorNonFatal(FirebaseLogServiceImplementation.kt: 27)
firebaseCrashlytics.recordException(
    java.lang.Exception: Exception Message: Backend API error
); 
-> at jp.co.rakuten.pointclub.android.services.log.FirebaseLogServiceImplementation.reportApiErrorNonFatal(FirebaseLogServiceImplementation.kt: 33)

Comparison Failure: <Click to see difference >

Argument(s) are different!
Wanted:
    firebaseCrashlytics.recordException(
        java.lang.Exception: Exception Message: Backend API error
    ); 
-> at com.google.firebase.crashlytics.FirebaseCrashlytics.recordException(FirebaseCrashlytics.java: 284)
Actual invocations have different arguments:
    firebaseCrashlytics.setCustomKey(
        "Exception Message: ",
        "Backend API error"
    ); 
-> at jp.co.rakuten.pointclub.android.services.log.FirebaseLogServiceImplementation.reportApiErrorNonFatal(FirebaseLogServiceImplementation.kt: 27)
firebaseCrashlytics.recordException(
    java.lang.Exception: Exception Message: Backend API error
); 
-> at jp.co.rakuten.pointclub.android.services.log.FirebaseLogServiceImplementation.reportApiErrorNonFatal(FirebaseLogServiceImplementation.kt: 33)

at com.google.firebase.crashlytics.FirebaseCrashlytics.recordException(FirebaseCrashlytics.java: 284)

这里显示比较失败。但是我不明白firebaseCrashlytics.setCustomKey是如何进行比较的,因为我只需要验证firebaseCrashlytics.recordException

如果有人知道我做错了什么。这对我会有很大的帮助。提前致谢。

4

0 回答 0