【问题标题】:ArgumentCaptor - Argument(s) are differentArgumentCaptor - 参数不同
【发布时间】:2019-05-03 17:05:27
【问题描述】:

我只是想检查用户是否是使用异步方法创建的,我是测试新手,我正在使用 ArgumentCaptor 检查是否调用了 onRegistrationSucces() 回调 1 次来检查它成功了。

这就是我所做的测试

RegisterTest.kt

@Test
    fun should_SignUpUser(){
        presenter.signUp("test1","test1@gmail.com","asdasd")
        verify(interactor).createUserWithEmailAndPassword("test1","test1@gmail.com","asdasd",object: RegisterInteractor.RegisterCallBack{
            override fun onRegistrationSucces() {
                callbackCaptor.capture()
            }

            override fun onRegistrationFailure(errorMsg: String) {
                callbackCaptor.capture()
            }
        })
        verify(callbackCaptor.value.onRegistrationSucces(), times(1))
    }

这是我正在尝试测试的演示者方法

RegisterPresenter.kt

override fun signUp(fullName:String, email: String, password: String) {
        view?.showProgress()
        registerInteractor.createUserWithEmailAndPassword(fullName,email, password, object : RegisterInteractor.RegisterCallBack {

                override fun onRegistrationSucces() {
                    if(isViewAttached()){
                        view?.navigateToLogin()
                        view?.hideProgress()
                    }
                }

                override fun onRegistrationFailure(errorMsg:String) {
                    if(isViewAttached()){
                        view?.showError(errorMsg)
                        view?.hideProgress()
                    }
                }

            })
    }

但是我收到了这个错误

参数不同!通缉: interactor.createUserWithEmailAndPassword( “测试1”, "test1@gmail.com", “asdasd”, com.testapp.presentation.register.presenter.RegisterPresenterTest$should_SignUpUser$1@c86b9e3 ); -> 在 com.testapp.presentation.register.presenter.RegisterPresenterTest.should_SignUpUser(RegisterPresenterTest.kt:119) 实际调用有不同的参数: interactor.createUserWithEmailAndPassword( “测试1”, "test1@gmail.com", “asdasd”, com.testapp.presentation.register.presenter.RegisterPresenter$signUp$1@10aa41f2 ); -> 在 com.testapp.presentation.register.presenter.RegisterPresenter.signUp(RegisterPresenter.kt:64)

【问题讨论】:

    标签: android unit-testing kotlin


    【解决方案1】:
    inner class below:          
    
    class CallbackRegister extends RegisterInteractor.RegisterCallBack {  
    private View view;
    private Object forViewattached;     
    
    public CallbackRegister(Object forViewattached, View view){
    this.forViewattached=forViewattached;
    this.view = view;
    }
    
     override fun onRegistrationSucces() {
                        if(forViewattached.isViewAttached()){
                            view?.navigateToLogin()
                            view?.hideProgress()
                        }
                    }
    
                    override fun onRegistrationFailure(errorMsg:String) {
                        if(forViewattached.isViewAttached()){
                            view?.showError(errorMsg)
                            view?.hideProgress()
                        }
                    }
    
    
    //end class
    }
    
    
    Access this by the  kotlin equivalent:
    
    object : new CallbackRegister(forViewattached, view)   
    
    instead of object: RegisterInteractor.RegisterCallBack{...}
    
    you can easily test this by:   TopLevelClass.CallbackRegister callbackRegisterUnderTest = new TopLevelClass().new CallbackRegister(MockforViewattached, MockView);
    
    You can now call the callback methods directly and verify the mocks:
    
    callbackRegisterUnderTest.onRegistrationSucces()
    verify the mocks did something.
    You have to convert this to kotlin, but I hope you see what is happening.
    

    【讨论】:

      【解决方案2】:

      问题是每个 createuserwithemailandpasswords 方法的回调是不同的。这就是您看到“参数不同”错误的原因。

      使用以下方法正确捕获回调:

      verify(interactor).createUserWithEmailAndPassword(anyString(),anyString(),anyString(),callbackCaptor.capture())
                  verify(callbackCaptor.value.onRegistrationSucces(), times(1))
      

      如果您想验证它们,可以将 anyString 替换为唯一的字符串捕获器。

      【讨论】:

      • 是的,我以前这样做过,但是从几天开始就遇到了错误callbackCaptor.capture() must not be null,所以这就是我更改实现的原因。
      • ArgumentCaptor 的定义@Captor private lateinit var callbackCaptor: ArgumentCaptor<RegisterInteractor.RegisterCallBack>
      • 下一步,因为我看不到您用于初始化交互器的所有代码,所以要捕获其中一个字符串值并验证它不为 null 并且符合您的期望。如果它与“test1”、“test1@gmail.com”或“asdasd”不匹配,则交互器存在初始化问题。接下来验证。
      • 此外,您不会看到callbackCaptor.value.onRegistrationSuccess 被调用,因为它没有在代码中执行。为了使回调可测试,我通常将它们添加为单独的公共类或内部类并单独调用覆盖方法。
      • 下面的内部类:
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-13
      • 1970-01-01
      • 2015-06-16
      相关资源
      最近更新 更多