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

Make pending tasks unit testable #57

Open
levibostian opened this issue Apr 1, 2020 · 0 comments
Open

Make pending tasks unit testable #57

levibostian opened this issue Apr 1, 2020 · 0 comments

Comments

@levibostian
Copy link
Owner

Here is an example PendingTask of mine:

class FavoriteWorkoutPendingTask(workout: Workout?): PendingTask(
        dataId = if (workout != null) JsonAdapter.toJson(workout) else null,
        groupId = null,
        manuallyRun = false,
        tag = TAG) {

    @Inject lateinit var userManager: UserManager
    @Inject lateinit var logger: Logger
    @Inject lateinit var api: ServiceApi

    companion object {
        const val TAG = "Favorite workout"

        fun blank(graph: AppGraph): FavoriteWorkoutPendingTask = FavoriteWorkoutPendingTask(null).apply {
            graph.inject(this)
        }

        // For testing only
        fun blank(userManager: UserManager, logger: Logger, api: ServiceApi): FavoriteWorkoutPendingTask = FavoriteWorkoutPendingTask(null).apply {
            this.userManager = userManager
            this.logger = logger
            this.api = api
        }
    }

    override fun isReadyToRun(): Boolean = userManager.isUserLoggedIn

    override fun runTask(): PendingTaskResult {
        val workout: Workout = JsonAdapter.fromJson(this.dataId!!)

        val response = api.favoriteWorkout(workout.id, workout.favorite)
                .subscribeOn(Schedulers.io())
                .blockingGet()

        return if (response.isFailure) PendingTaskResult.FAILED else PendingTaskResult.SUCCESSFUL
    }

}

This PendingTask requires dependencies. The current way to provide them is quite hacky. I must make 2 constructors where 1 constructor is used for the app implementation and 1 is a tedious one where I can inject mocked instances of the dependencies.

There might be a way to include this issue for improving both with a better API.

The PendingTask subclasses are pretty simple. They just need to tell Wendy if your task can run, how to run it, etc. This class should be fully unit testable. It's up to Wendy to store the data about it.

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

No branches or pull requests

1 participant