Skip to content

Instantly share code, notes, and snippets.

@ArunYogeshwaran
Last active February 6, 2023 13:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ArunYogeshwaran/b8b9e7fe40c3a9c049b91d96f7052638 to your computer and use it in GitHub Desktop.
Save ArunYogeshwaran/b8b9e7fe40c3a9c049b91d96f7052638 to your computer and use it in GitHub Desktop.
An example of testing the interaction vs testing the state
// Unit under test
fun toggleFavoriteForJob(job: Job) {
if (job.isFavorite) {
cache.unfavorite(job)
} else {
cache.favorite(job)
}
}
// A unit test using interaction testing
fun `toggle favorite with already favorited job verify job is unfavorited`() {
// Test arrangements and setup
val job = getTestFavoriteJob()
jobOperations.toggleFavoriteForJob(job)
verify(cache).unfavorite(job)
}
// A unit test using state testing
fun `toggle favorite with already favorited job verify job is unfavorited`() {
// Test arrangements and setup
val job = getTestFavoriteJob()
jobOperations.toggleFavoriteForJob(job)
assertThat(cache.getJobWithId(job.id).isFavorite()).isFalse()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment