Skip to content

Make pending tasks unit testable #57

Open
@levibostian

Description

@levibostian

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @levibostian

        Issue actions

          Make pending tasks unit testable · Issue #57 · levibostian/Wendy-Android