vendredi 7 juin 2019

TDD Unit Test sub-methods

Hi im in a delima of whether I'll write tests for methods that are a product of refactoring another method.

First question, consider this plot.

class Puzzle(
    val foo: List<Pieces>,
    val bar: List<Pieces>
) {
    init {
       // code to validate foo
       // code to validate bar
    }
}

Here im validating parameters in constructing an object. This code is the result of TDD. But with TDD we write fail_test -> pass test -> refactor, when refactoring I tranfered validator methods to a helper class PuzzleHelper.

object PuzzleHelper {

    fun validateFoo() {
         ...
    }

    fun validateBar() {
         ...
    }
}

Do I still need to test validateFoo and validateBar in this case?

Second question

class Puzzle(
    val foo: List<Pieces>,
    val bar: List<Pieces>
) {
    ...

    fun getPiece(inPosition: Position) {
        validatePosition()
        // return piece at position
    }

    fun removePiece(inPosition: Position) {
        validatePosition()
        // remove piece at position
    }
}

object PuzzleHelper {

    ...

    fun validatePosition() {
         ...
    }
}

Do I still need to write test for getPiece and removePiece that involve position validation?

I really want to be fluent in using tdd, but dont know how to start. Now I finally dive-in and don't care whats ahead, all I want is product quality. Hope to hear from your enlightenment soon.

Aucun commentaire:

Enregistrer un commentaire