我有一个Kotlin Singleton类,该类保持我要在给定Test方法中测试具有不同状态的此类的状态。
object CreateChallengeState {
/*
* Started Challenge
*
* */
var startedChallengeLiveData = MutableLiveData<String?>()
/*
*
* Challenge Duration Data
*
* */
data class ChallengeDate(val start: Date? = null, val end: Date? = null)
@JvmField
var challengeDateLiveData = MutableLiveData<ChallengeDate>()
var startDateShortLiveData: LiveData<String?> = Transformations.map(challengeDateLiveData) {
it?.start?.let { date -> getShortDateTime(date) }
}
var startDateLiveData: LiveData<String?> = Transformations.map(challengeDateLiveData) {
it?.start?.let { date -> getDateTime(date) }
}
var endDateShortLiveData: LiveData<String?> = Transformations.map(challengeDateLiveData) {
it?.end?.let { date -> getShortDateTime(date) }
}
var endDateLiveData: LiveData<String?> = Transformations.map(challengeDateLiveData) {
it?.end?.let { date -> getDateTime(date) }
}
var endTimeStampData: LiveData<Long?> = Transformations.map(challengeDateLiveData) {
it?.end?.let { date -> date.time }
}
/*
*
* Customize Challenge
*
* */
val title = MutableLiveData<String?>().also { it.value = "" }
val color = MutableLiveData<String?>().also { it.value = "" }
val userUrl = MutableLiveData<String?>().also { it.value = "" }
val userName = MutableLiveData<String?>().also { it.value = "" }
val globalHeaderUrl = MutableLiveData<String?>().also { it.value = "" }
val challengeMission = MutableLiveData<String?>().also { it.value = "" }
val challengeTarget = MutableLiveData<String?>().also { it.value = "" }
/*
*
* Challenges States
*
* */
var isDateEnable: LiveData<Boolean> = startDateLiveData.combine(endDateLiveData) { a, b ->
a != null && a.isNotBlank() && b != null && b.isNotBlank()
}
var isCustomizationLocalButtonEnable: LiveData<Boolean> =
combine(title, color, userUrl, userName) { title,
color,
userUrl,
userName ->
title != null && title.isNotBlank() &&
color != null && color.isNotBlank() &&
userUrl != null && userUrl.isNotBlank() &&
userName != null && userName.isNotBlank()
}
}
`@Test
fun isDurationButtonEnable() {
val challengeState = spyk<CreateChallengeState>()
challengeState.challengeDateLiveData.value = currentDate
assertEquals(challengeState.isDateEnable.getOrAwaitValue(), true)
}
@Test
fun isDurationButtonShouldNotEnable_1() {
val challengeState = spyk<CreateChallengeState>()
challengeState.challengeDateLiveData.value = endDateNull
assertEquals(challengeState.isDateEnable.getOrAwaitValue(), false)
}`
我该如何测试该类,我想为每个测试方法创建不同的CreateChallengeState对象,但由于类是单例,它也在其他方法中更改了我不想要的CreateChallengeState的状态。