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

#9 Unit Testing #33

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

lito-bumba
Copy link

Unit Testing

  • Write unit tests for repository class.
  • Focus on data retrieval, processing, and storage logic.
  • Use fakes data.
  • Ensure the repository works independently of UI.

@lito-bumba lito-bumba mentioned this pull request Jul 30, 2024
@jarvislin jarvislin self-assigned this Jul 30, 2024
import kotlinx.coroutines.flow.flow
import kotlinx.serialization.json.Json

class FakeItemsRepository(
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the purpose of FakeItemsRepository?

@@ -61,6 +61,10 @@ kotlin {
implementation(libs.compose.webview.multiplatform)
implementation(libs.richeditor.compose)
}
commonTest.dependencies {
implementation(libs.kotlin.test)
implementation("app.cash.turbine:turbine:0.7.0")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You haven't used implementation("app.cash.turbine:turbine:0.7.0") in your changes; it can be added when needed.

Comment on lines +23 to +31
// val result = if (!isComment) {
// """
// {"by":"test","descendants":1,"id":$id,"kids":[1],"score":1,"time":1720481386,"title":"test","type":"story","text":"test","url":"test"}
// """
// } else {
// """
// {"by":"test", "id":$id, "kids":[1], "parent": 1, "time":1720481386,"title":"test","type":"comment","text":"test"}
// """
// }
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary comments can be removed.

Copy link
Owner

@jarvislin jarvislin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the changes.

The FakeItemsRepository is the main blocker, duplicating many codes from ItemsRepositoryImpl. Why do we need to do this?

We can write tests for ItemsRepositoryImpl and use mock for your needs.

For the next steps, I'd recommend you research mock libs, I am familiar with mockk and mockito but I'm not sure if they support KMP projects. So maybe you can do this for me and make the new changes after your research.

@lito-bumba
Copy link
Author

Fake Repository is an alternative for mocking data, I used this alternative because neither Mockito nor Mockk support Kotlin Multiplatform tests

The current secure alternative is to mock values by yourself, this is the approach of Fake Repository

@jarvislin
Copy link
Owner

Update

I don't think the approach is the most suitable solution. I checked out https://github.com/terrakok/kmp-awesome#-test, and most of them don't support Kotlin 2.0.0. 😢

I'd like to hold off for now and explore if there's a better solution available.

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

Successfully merging this pull request may close these issues.

2 participants