Is it OK to copy & paste unit-tests when the logic is basically the same?

前端 未结 9 2049
悲&欢浪女
悲&欢浪女 2021-02-02 12:09

I currently have like 10 tests that test whenever my Tetris piece doesn\'t move left if there is a piece in the path, or a wall. Now, I will have to test the same behaviour for

9条回答
  •  北荒
    北荒 (楼主)
    2021-02-02 12:38

    Try taking the 3rd approach that you haven't mentioned, that of refactoring your code so that you can share one implementation of the test between all 10 tests.

    The jist is, duplicating code is almost always the wrong thing to do. In this example you could refactor the checking code into a method called, for example IsTetrisPieceUnableToMoveLeftBecauseOfAPieceOrAWall. I always go for very descriptive method names like that when writing a bit of "shared" functionality for a unit test as it makes it extraordinarily clear just what's being done / tested.

提交回复
热议问题