scribe-org / Scribe-Android

Android app with keyboards for language learners
GNU General Public License v3.0
39 stars 38 forks source link

Add examples of tests using mocking with MockK #197

Open albendz opened 2 weeks ago

albendz commented 2 weeks ago

Terms

Description

Summary

The Scribe Android app supports unit testing with JUnit and mocking with MockK. Right now there aren't any test examples using mocking.

Add an example test that uses mocking.

Note that this may require some refactoring if you find you can't mock certain things or you're mocking a lot. For example, you cannot mock getting environment variables so that can be extracted into a separate class that itself can be mocked. I encountered this issue when I was attempting to write a unit test for one of the Activity classes.

Additional details

Here are some details related to mocking for Android: https://developer.android.com/training/testing/local-tests#mocking-dependencies

MockK documentation: https://mockk.io/

Contribution

I can pick this up later if no one else gets to it before I'm free

KesharwaniArpita commented 1 week ago

Hi @albendz I am interested in this issue!

albendz commented 1 week ago

@andrewtavis Could you take a look at assigning this to @KesharwaniArpita ?

andrewtavis commented 1 week ago

Thanks for the ping, @albendz :)

@KesharwaniArpita, could we ask you to send along a plan for this issue and we'll give you the ok to implement it? Please let us know if you need some support :)

CC @angrezichatterbox

KesharwaniArpita commented 1 week ago

HI @albendz @andrewtavis do you think if we should consider writing tests for the MainActivity using mockk to ensure the functionality works as expected without relying on actual dependencies? We can start by mocking components like SharedPreferences, we can verify that user preferences, such as dark mode settings, are correctly applied in the applyUserDarkModePreference() method. Does that sound good?

angrezichatterbox commented 1 week ago

HI @albendz @andrewtavis do you think if we should consider writing tests for the MainActivity using mockk to ensure the functionality works as expected without relying on actual dependencies? We can start by mocking components like SharedPreferences, we can verify that user preferences, such as dark mode settings, are correctly applied in the applyUserDarkModePreference() method. Does that sound good?

Yes, It sounds good to me. You could write tests for the Main Activity as an example.