Skip to content

Instantly share code, notes, and snippets.

@ArunYogeshwaran
Created July 6, 2023 14:02
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/85de87179586d87beb14943e1058e99f to your computer and use it in GitHub Desktop.
Save ArunYogeshwaran/85de87179586d87beb14943e1058e99f to your computer and use it in GitHub Desktop.
The example below shows a system that toggles the "favorite" state of a given job in the cache.
// 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 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