Skip to content

How can I create unit test for datasources in a clean architecture project?Β #88

@joragua

Description

@joragua

Hello MocKMP team! 🍻 Right now, I'm using a clean architecture for my Kotlin Multiplatform project (Android/iOS). I'm able to create unit test for repositories and usecases because all of them have dependencies with interfaces but I am not able for datasources.

For example: in my project, the Repository has two different classes in its constructor (IRemoteDataSource and ILocalDataSource) so I can mock and use them in tests. The same happens with usecases, because they have dependencies with repositories interfaces so, there is no problem to create mocks. Here, an example that I've in my repository:

...

@Mock 
lateinit var localDataSource: LocalDataSource
@Mock
lateinit var remoteDataSource: RemoteDataSource

private val repository by withmocks { Repository(localDataSource, remoteDataSource) }

@Test
fun test() {
  ...
}

Now, the problem is: how can I create tests for datasources using MocKMP? LocalDataSource has dependency with my database class (which is a interface) but, at the same time, it has dependency with an auto-generated class called FolderQueries which has the methods that I want to test.

class LocalDataSource(database: AppKMMPoCDB): ILocalDataSource {

    private val queries = database.folderQueries

    override fun getAllFolders(): Flow<List<Folder>> = queries.getAllFolders()
}

getAllFolders() is the method that I want to test but, how can it be possible? Maybe, there is no solution for this implementation... πŸ™ƒ

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions