Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/178.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_Testing_Kotlin_Mockito - Fatal编程技术网

Android 模拟方法错误

Android 模拟方法错误,android,testing,kotlin,mockito,Android,Testing,Kotlin,Mockito,我尝试模拟项目中的一些方法,以便在调用它们时返回特定的值。 但当您运行测试时,它们会随输出一起下降: org.mockito.exceptions.misusing.invalidUseofMatchers异常: 参数匹配器的使用无效!应为0个匹配器,记录了1个: ->在com.hodzi.stackviewer.questions.detail.QuestionDetailPresenterTest.voteTest(QuestionDetailPresenterTest.kt:69)上 如果

我尝试模拟项目中的一些方法,以便在调用它们时返回特定的值。 但当您运行测试时,它们会随输出一起下降:

org.mockito.exceptions.misusing.invalidUseofMatchers异常: 参数匹配器的使用无效!应为0个匹配器,记录了1个: ->在com.hodzi.stackviewer.questions.detail.QuestionDetailPresenterTest.voteTest(QuestionDetailPresenterTest.kt:69)上

如果匹配器与原始值组合,则可能发生此异常: //不正确: someMethod(anyObject(),“原始字符串”);使用匹配器时,所有参数都必须由匹配器提供。例如: //正确: someMethod(anyObject(),eq(“匹配器字符串”)

如果在调试模式下运行相同的代码并遍历所有行,那么当调用shared.getToken()时,将返回我们指定的值。但正常启动时,测试就落在这条线上

代码:

共享:

interface Shared {
    companion object {
        const val KEY_TOKEN: String = "keyToken"
    }

    fun getToken(): String

    fun saveToken(token: String?)
}
演示者:

class QuestionDetailPresenter(val questionsInteractor: QuestionsInteractor, val shared: Shared) :
    BasePresenter<QuestionDetailView>() {
    lateinit var question: Question

    fun vote(id: Int, vote: Vote) {
        print(vote)
        if (Strings.isEmptyString(shared.getToken())) {
            view?.goToAuth()
            return
        }

        val observable: Observable<out Data> = when (vote) {
            Vote.ANSWER_UP     -> {
                questionsInteractor.answerUpVote(id, shared.getToken())
            }
            Vote.ANSWER_DOWN   -> {
                questionsInteractor.answerDownVote(id, shared.getToken())
            }
            Vote.QUESTION_UP   -> {
                questionsInteractor.questionUpVote(id, shared.getToken())
            }
            Vote.QUESTION_DOWN -> {
                questionsInteractor.questionDownVote(id, shared.getToken())
            }
        }
        baseObservableData(observable,
            { data ->
                run {
                    Log.d(Const.LOG_TAG, "success")
                }
            },
            { throwable ->
                run {
                    Log.d(Const.LOG_TAG, "error")
                }
            }
        )
    }
}
class QuestionDetailPresenter(val questionsInteractor:questionsInteractor,val shared:shared):
BasePresenter(){
lateinit变量问题:问题
趣味投票(id:Int,投票:投票){
打印(投票)
if(Strings.isEmptyString(shared.getToken())){
视图?.gotouth()
返回
}
val可观察:可观察=何时(投票){
投票。回答_UP->{
questionsInteractor.answerUpVote(id,shared.getToken())
}
投票。回答你的问题->{
questionsInteractor.answerDownVote(id,shared.getToken())
}
投票。问题\向上->{
questionsInteractor.questionUpVote(id,shared.getToken())
}
投票。问题_否决->{
questionsInteractor.questionDownVote(id,shared.getToken())
}
}
基本可观测数据(可观测,
{数据->
跑{
Log.d(Const.Log_标记“success”)
}
},
{可丢弃->
跑{
Log.d(Const.Log_标记“error”)
}
}
)
}
}

谢谢

你嘲笑共享的
没有什么错,我认为问题在于:

 presenter.vote(ArgumentMatchers.anyInt(), Vote.QUESTION_DOWN)
只需使用实数
Int
而不是
ArgumentMatchers.anyInt()
。 像

例如,匹配器用于匹配模拟对象上的参数

val calulator = (mock with Mockito)
when(calculator.divideByTwo(anyInt()).thenReturn(1)
将表示
calculator.divideByTwo(int:int)
在使用任何
int
调用时返回
1

当调用真实对象的方法来测试它们时(就像您对演示者所做的那样),您使用的是真实参数

presenter.vote(0, Vote.QUESTION_DOWN)
val calulator = (mock with Mockito)
when(calculator.divideByTwo(anyInt()).thenReturn(1)