I have a run function which calls repository.startFlling(orderStorage.flOrder!!.id, action)
and I am trying to verify repository.startFlling
is called with correct parameters.
I could mock orderStorage.flOrder!!.id
but action
gets created locally in the function and is based on DateTime
,
I tried creating an action in my testcase but that does not match with the one dynamically created in the actual function.
how can I deal with this scenario/
Here is my run function with startTime
which gets created when called.
val startTime = DateTime()
lateinit var orderEquipment: Equipment
override suspend fun run(params: Params): Either<Failure, GenericResponse> {
this.orderEquipment = params.equipment
val action = TimestampedAction(
app.session.user.id, null, startTime
)
val result = repository.startFlling(orderStorage.flOrder!!.id, action)
result.fold(::handleStartFllingFailure, ::handleStartFllingSuccess)
return result
}
Test case
@Test
fun `when StartFllingUseCase is invoked then call startFlling in flOrderRespository with correct parameters`() {
val id = "1"
val userId = "userId"
val flOrderId = "flOrderId"
val action: TimestampedAction = TimestampedAction(userId, null, DateTime())
val equipment: Equipment = mock()
runBlocking {
whenever(user.id).thenReturn(userId)
whenever(orderStorage.flOrder).thenReturn(flOrder)
whenever(flOrder.id).thenReturn(flOrderId)
whenever(equipment.times).thenReturn(times)
whenever(flOrderRepository.startFlling(any(), any()))
.thenReturn(Either.Right(GenericResponse(true)))
startFllingUseCase.run(StartFllingUseCase.Params(equipment))
verify(flOrderRepository).startFlling(flOrderId, action)
}
}
error
org.mockito.exceptions.base.MockitoAssertionError: There were multiple verification failures:
1. Argument(s) are different! Wanted:
flOrderRepository.startFlling(
"flOrderId",
com.xx.xxx.objects.florder.equipment.TimestampedAction@4567e53d
);
-> at com.xx.xxx.clean.florder.data.repository.FlOrderRepository.startFlling(FlOrderRepository.kt:17)
Actual invocations have different arguments:
flOrderRepository.startFlling(
"flOrderId",
com.xx.xxx.objects.florder.equipment.TimestampedAction@66ec9390
);
Can you please suggest how can I fix this
thanks
R
question from:
https://stackoverflow.com/questions/65928879/how-to-verify-a-parameter-in-unit-test 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…