Step #9: Adding Another Test Function
That tests our motor’s states — we should also test the actions.
Add this test function to SingleModelMotorTest
:
@Test
fun `actions pass through to repo`() {
val replacement = testModel.copy("whatevs")
underTest.save(replacement)
mainDispatcherRule.dispatcher.runCurrent()
runBlocking { verify(repo).save(replacement) }
underTest.delete(replacement)
mainDispatcherRule.dispatcher.runCurrent()
runBlocking { verify(repo).delete(replacement) }
}
Here, we are confirming that we can save and delete properly. To do this, we need to determine if save()
and delete()
on the motor are calling save()
and delete()
on the repository. Since our repository is a mock, it tracks all calls made to it during a test run. We can then have test code check to see if the calls were made as expected.
So, we call save()
and delete()
on our SingleModelMotor
that we are testing. After each of those calls, we call mainDispatcherRule.dispatcher.runCurrent()
, to ensure that the actual work executes, now that we are ready for it to do so.
We then want to verify that our repository was called to save and delete those models. verify()
, from Mockito, lets us see if a particular call was made on our mock. To do this, we pass the mock()
to verify, then make the call that we want verified on the ToDoRepository
object returned by verify()
. This will either work or fail with an assertion error.
However, since save()
and delete()
in our repository are suspend
functions, though, we need to run them inside of some CoroutineScope
, even though those are really mock functions. runBlocking()
will suffice for our mock call verification.
If you run all the tests on SingleModelMotorTest
, they should all succeed.
Prev Table of Contents Next
This book is licensed under the Creative Commons Attribution-ShareAlike 4.0 International license.