SpannableString不';不要在Android单元测试上工作

SpannableString不';不要在Android单元测试上工作,android,unit-testing,kotlin,testing,spannablestring,Android,Unit Testing,Kotlin,Testing,Spannablestring,我尝试在单元测试中比较两个spannablestring文本,但收到错误: java.lang.RuntimeException: Method toString in android.text.SpannableString not mocked. See http://g.co/androidstudio/not-mocked for details. 调试之后,我验证了我根本不能在单元测试中使用SpannableString类 测试实施: @测试 乐趣`当getCredits()返回多个

我尝试在单元测试中比较两个spannablestring文本,但收到错误:

java.lang.RuntimeException: Method toString in android.text.SpannableString not mocked. See http://g.co/androidstudio/not-mocked for details.
调试之后,我验证了我根本不能在单元测试中使用SpannableString类

测试实施:

@测试
乐趣`当getCredits()返回多个邀请时`()=testDispatcher.testCoroutineDispatcher.runBlockingTest{
//更改模拟响应
repository.setMultipleInvitesResponse()
//调用ViewModel方法返回积分
testDispatcher.testCoroutineDispatcher.pauseDispatcher()
viewModel.getCredits()
testDispatcher.testCoroutineDispatcher.resumeDispatcher()
//获取远程SpannableString邀请
val invests=“您有5个邀请”
//初始化viewModel.showInvitesText.value
val showInvitesTextValue:SpannableString=viewModel.showInvitesText.value
//将视图模型showInvitesText.value与邀请进行比较
assertEquals(showInvitesTextValue.toString(),invites)
}
堆栈跟踪:

java.lang.RuntimeException: Method toString in android.text.SpannableString not mocked. See http://g.co/androidstudio/not-mocked for details.

at android.text.SpannableString.toString(SpannableString.java)
at java.lang.String.valueOf(String.java:2994)
at view_model.ViewModelTest$When getCredits() returns multiple invites$1.invokeSuspend(ViewModelTest.kt:113)
at ViewModelTest$When getCredits() returns multiple invites$1.invoke(ViewModelTest.kt)
at kotlinx.coroutines.test.TestBuildersKt$runBlockingTest$deferred$1.invokeSuspend(TestBuilders.kt:50)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
at kotlinx.coroutines.test.TestCoroutineDispatcher.dispatch(TestCoroutineDispatcher.kt:50)
at kotlinx.coroutines.internal.DispatchedContinuationKt.resumeCancellableWith(DispatchedContinuation.kt:305) ...
showInvitesTextValue变量初始化时出错


如何在单元测试中使用SpannableString类?

这是否回答了您的问题@emandt,不幸的是,你的回答无助于解决我的问题,因为我的问题是关于SpannableString导入,而不是关于equals比较。你说“你的问题是当你试图比较2个Spannables时”,并且StackStrace非常清楚地指出了出现异常的地方:在“SpannableString.equals()”。如果从不调用“Spannable.equals()”,并不意味着库或组件不调用它;)@emandt,对不起,因为我从另一个测试中复制了stacktrace。我用write stacktrace编辑我的问题。为什么不打开stacktrace的建议链接?您将看到“android.jar”是空的,所以您应该模拟所有方法,否则它们将抛出异常。所有这些都在链接中得到了很好的解释。。。。(我个人从未使用过单元测试、模拟或其他类似的东西)