我正在尝试编写遵循干净架构方法的kotlin测试用例。我的主持人课看起来像这样:
class WeatherDetailsPresenter @Inject constructor(var getCurrentWeatherWithForecastUsecase: GetCurrentWithForcastedWeatherUsecase) : BaseMvpPresenter<WeatherDetailsView>() {
fun presentCurrentAndForcastedWeather(country: String?) {
with{getCurrentWeatherWithForecastUsecase) {
this.countyName = country
execute(object : DefaultSubscriber<Pair<CurrentWeatherModel, ForecastedWeatherModel>>() {
override fun onSubscribe(d: Disposable) {
super.onSubscribe(d)
showLoading(true)
}
override fun onNext(t: Pair<CurrentWeatherModel, ForecastedWeatherModel>) {
super.onNext(t)
view?.showCurrentWeather(t.first)
}
override fun onComplete() {
super.onComplete()
showLoading(false)
}
})
}
}
}
我想测试显示的当前天气。到目前为止,这是我为演讲者类进行的junit测试编写的内容:
@RunWith(MockitoJUnitRunner::class)
class WeatherDetailsPresenterTest {
lateinit var usecase: GetCurrentWithForcastedWeatherUsecase
@Mock
lateinit var repo: WeatherDataRepository
@Mock
lateinit var view: WeatherDetailsView
lateinit var presenter: WeatherDetailsPresenter
@Before
fun setUp() {
usecase = GetCurrentWithForcastedWeatherUsecase(repo)
presenter = WeatherDetailsPresenter(usecase)
}
//https://stackoverflow.com/questions/47083504/what-and-how-to-test-in-a-presenter-with-a-rxandroid-use-case
@Test
fun testCurrentWeatherScreenAppears() {
//arrange
//given has replaced "when" cmd, and will has replaced "then"
BDDMockito.given(usecase.execute((ArgumentMatchers.any()))).will { invocation ->
val observer = invocation.arguments[0] as DefaultSubscriber<Pair<CurrentWeatherModel, ForecastedWeatherModel>>
observer.onComplete() //i want to test that after this line is called, currentWeather is showing
}
//act
presenter.presentCurrentAndForcastedWeather("indonesia")
//assert
BDDMockito.then(view).should().showCurrentWeather(ArgumentMatchers.any())
}
}
当我处理这样的用例时,有没有更简单的方法来测试是否调用了视图代码?这是进行测试的正确方法吗?