Testing ViewModels - Testing in Android - Part 10

preview_player
Показать описание
In this video you will learn how you can test your ViewModels in Android with the help of a fake repository.

⭐ Get certificates for your future job
⭐ Save countless hours of time
⭐ 100% money back guarantee for 30 days
⭐ Become a professional Android developer now:

💻 Let me personally review your code and provide individual feedback, so it won't backfire and cost you a fortune in future:

🎓 Become an industry-ready developer in the Mobile Dev Campus:

🎁 Get my FREE 4.5h course "The Best Practice Guide to Android Architecture":

Read our weekly Android blog:

Get the source code for this video here:

Рекомендации по теме
Комментарии
Автор

Give this guy a nobel already. Outstanding work!!

aritrabhattacharya
Автор

Since I discovered this channel, my life has never been the same again. Excellent!

elijahonduso
Автор

At 1:52 The ShoppingDao is interface so we can make FakeShoppingDao class and implement the methods and then return fake data from their method, the same is for the PixabeyAPI it is also an interface, so we should make FakePixabeyAPI class and return fake api response. So we should make two separate tests
1) When we test the ViewModel we should use the same logic as you did and create FakeShoppingRepository
2) Create separate test for the DefaultShoppingRepository class using the FakeShopingDao and FakePixabeyAPI classes, that way we can test the logic inside the class

dependencies
The idea is that we should mainly use interfaces, and inject the classes using Hilt or Dagger, that way our tests are not depending on any other classes that we should pass to the constructor

slaviboy
Автор

This is great! Thank you, Philipp! ❤️ Your tutorial playlist has been a life-saver for testing in Android.

damilolaomoyiwola
Автор

Thank you so much philipp! i was struggling so much with the concept of testing as long as i could remember, I'm so happy i found your channel <3

sbane
Автор

Stop one of the greatest Android channel :) never forget 😉

iftak
Автор

Regarding setValue() and postValue() of LiveData the key difference is in which thread we call them. If we are in a background thread, we need to use postValue(). If we are in the main thread we can call directly setValue().

haykmkrtchyan
Автор

I just like all of your videos before I watch it, you are awesome bro and I think you deserve the most subscribers, anyway keep it up and help the developer from worldwide.

mehedihasansarik
Автор

I am crying. The topics selected are so helpful

AwesomeDevNotes
Автор

Thats a very interesting approach. You keeped it simple and well explained just like in the other videos, thank you very much!

fausto
Автор

Great video, surely a bit outdated but anyone can manage to replace the deprecated stuff

krutz
Автор

Great tutorial on such an important subject. Thank you!

funnymoviemomen
Автор

Again and again. Great content! Well explained!

hoonjung
Автор

First of all thank you for your great work!!! :)
Maybe you can give a quick answer to the following question:
As I can see there’s some work needed to change the dispatcher and to mock the db and API calls. What are the reasons why the view mode test should not be put in the android test folder ? I am just thinking if it was in the Android Testfolder, the circuit es of the viewModel can be used and for the db an InMemoryDB can be set up. So from my point of view this would be a more realistic approach on the view model test and would finally also cover some the database transactions related to the view mode... Is it maybe the independence of the test cases that would require them to not use the actual database and coroutines?

mtrx
Автор

great video, was looking for MainCoroutineRule a long time

fahrican
Автор

Thank you very much for the lesson! It's awesome!
Now I found that if I test id what initial number should it has:
@Test
fun `correct id is returned after add first shopping item, returns success`() {

viewModel.insertShoppingItem("name", "5", "3.0")


}

it shouldn't be a null as we've mentioned earlier in the ShoppingItem data class, but can be 0 or 1 or any Int we want to start our list from.

bro_chenzox
Автор

thank you for wonderful tutorial and in life only your videos making me complete my homework ..lol

akashdeepwadhwa
Автор

Yeah
Finally I'm the first viewer

gajendrapandeya
Автор

Philipp thank you for this awesome course and you teach us really good , but can you tell me what document is good to read beside your course? thank you.

amirdaryabak
Автор

Great video but in MainCouroutineRule some functions are deprecated we have to migrate to news declarations

cleawrence
visit shbcf.ru