我正在尝试在 android 上使用 Spek 框架编写测试。但我很困惑。按照代码,presenter 必须在方法 beforeEachTest 中进行初始化。并且必须在方法 afterEachTest 中清除条件。但是当我运行测试时,它在 mPresenter!!.setPass(PASSWORD, PASSWORD) 行中出现错误 NPE。但是如果你取消前面两行的注释,那么测试就会成功。可能是什么问题呢?提前感谢您的回答
object ResetPasswordPresenterTestKotlin : Spek({
include(RxSchedulersOverrideSpek)
val PASSWORD = "123456aZ";
var mView = mock<ResetPasswordView>{}
var mMockDataManager = mock<DataManager>{}
var mToken = mock<SuccessDetails>{}
var mPresenter : ResetPasswordPresenter ?= null
beforeEachTest {
print("beforeEachTest")
mPresenter = ResetPasswordPresenter(mMockDataManager)
mPresenter!!.attachView(mView)
}
afterEachTest {
print("afterEachTest")
mPresenter!!.detachView()
reset(mView)
}
describe("Reset password is succesful") {
given(mMockDataManager.setNewPassword(any())).willReturn(Observable.just(mToken))
/*mPresenter = ResetPasswordPresenter(mMockDataManager)
mPresenter!!.attachView(mView)*/
mPresenter!!.setPass(PASSWORD, PASSWORD)
it("show loading progress") {
verify(mView).showLoadingProgress()
}
it("hide loading progress") {
verify(mView).hideLoadingProgress()
}
it("show on success") {
verify(mView).onSuccess()
}
it("no show error") {
verify(mView, never()).showError(RuntimeException())
}
}
})