Skip to content

Instantly share code, notes, and snippets.

@brunogabriel
Created October 5, 2021 20:01
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 brunogabriel/7e8fe17f99061e9b5aaafbb9f1567d7d to your computer and use it in GitHub Desktop.
Save brunogabriel/7e8fe17f99061e9b5aaafbb9f1567d7d to your computer and use it in GitHub Desktop.
Testes para o PostsViewModel
@Test
fun `should take posts and display it when call takePosts`() {
// given
val posts = listOf<Post>(mockk(), mockk())
val observerDisplayingView = spyk<Observer<Int>>()
val displayingViewResults = mutableListOf<Int>()
val observerPostResult = spyk<Observer<List<Post>>>()
val postResults = mutableListOf<List<Post>>()
every { useCase.takePosts() } returns Single.just(posts)
viewModel.displayingView.observeForever(observerDisplayingView)
viewModel.postResult.observeForever(observerPostResult)
// when
viewModel.takePosts()
// then
verify { observerDisplayingView.onChanged(capture(displayingViewResults)) }
verify { observerPostResult.onChanged(capture(postResults)) }
assertThat(displayingViewResults).isEqualTo(
listOf(
PostsViewModel.DisplayingView.Loading.ordinal,
PostsViewModel.DisplayingView.Success.ordinal
)
)
assertThat(postResults.size).isEqualTo(1)
assertThat(postResults.first()).isEqualTo(posts)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment