Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ViewModel tests are always successful #27

Open
jmcaldera opened this issue May 29, 2018 · 5 comments
Open

ViewModel tests are always successful #27

jmcaldera opened this issue May 29, 2018 · 5 comments

Comments

@jmcaldera
Copy link

Assertions made inside the observeForever block are always successful, even when they shouldn't.

For example in MoviesViewModelTest, if i say that the first element poster is equals to "IronMannnn", it will pass, even if the real value is "IronMan".

@JavierSA
Copy link

If you set a breakpoint inside the block of observeForever and debug, you'll see that it won't be reached. The reason is that GetMovies is mocked but the test gives behaviour only to run, so its method execute does nothing when is called by MoviesViewModel.

A solution might be spy GetMovies, so real methods are called (except run, which will be stubbed):

import org.mockito.Mockito.mock
import org.mockito.Mockito.spy

private val getMovies = spy(GetMovies(mock(MoviesRepository::class.java)))

@android10
Copy link
Owner

Good catch!

@JavierSA have you tried that out? If so, I would appreciate a PR.

Thanks!

@fowlerwill
Copy link

@JavierSA - Unfortunately, in my project, UseCases cannot be run (due to networking dependencies) so, I had to mock the UseCases. My PR presents an alternate solution.

@JavierSA
Copy link

JavierSA commented Sep 26, 2018

@fowlerwill I think yours is better, so I've closed my PR.

@android10
Copy link
Owner

Thanks everyone for the contribution! I will review those PRs asap. ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants