It is common for me to work with a state class (a sealed class) that I use to control the state of my screen (let's say Success, Error and Loading). So I'd have something like:
sealed class State {
object Loading: State()
object Success : State()
object Error : State()
}
Now, on some class, I have a propertie with this value that, in a certain flow, I set it to Loading and aftwards to Error. This I can check if that's the sequence this property is being set using verifySequence
like this:
verifySequence {
stateObserver.onChanged(State.Loading)
stateObserver.onChanged(State.Success)
}
Now let's say my sealed classes have classes inside of it instead of objects and is a little more complex, like so:
sealed class NetworkResult<T> {
class Success<T>(val data: T) : NetworkResult<T>()
class Error<T>(val message: String) : NetworkResult<T>()
class Loading<T> : NetworkResult<T>()
}
When I tried to check the sequency, my test fails because, since I'm using class and MockK makes a assert by referece:
verifySequence {
stateObserver.onChanged(State.Loading())
stateObserver.onChanged(State.Success(list))
}
Any idea how I can check this sequence?
Try using a data class instead of a simple class:
sealed class NetworkResult<T> {
data class Success<T>(val data: T?) : NetworkResult<T>()
data class Error<T>(val message: String?) : NetworkResult<T>()
data class Loading<T>(val nothing: Nothing? = null) : NetworkResult<T>()
}