How does TDD make refactoring easier?

前端 未结 8 890
温柔的废话
温柔的废话 2021-02-01 16:53

I\'ve heard that projects developed using TDD are easier to refactor because the practice yields a comprehensive set of unit tests, which will (hopefully) fail if any change has

8条回答
  •  再見小時候
    2021-02-01 17:30

    Under what situations in TDD is it okay to alter and delete test cases? How can you be sure that altering the test cases don't break them? Plus it seems that having to synchronize a comprehensive test suite with constantly changing code would be a pain.

    The point of tests and specs is to define the correct behaviour of a system. So, very simply:

    if definition of correctness changes
      change tests/specs
    end
    
    if definition of correctness does not change
      # no need to change tests/specs
      # though you still can for other reasons if you want/need
    end
    

    So, if application/system specifications or desired behaviour changes, it is a necessity to change the tests. Changing only the code, but not the tests, in such a situation is obviously broken methodology. You might look at it as "a pain" but having no test suite is more painful. :) As others have mentioned, having that freedom to "dare" to change code is very empowering and liberating indeed. :)

提交回复
热议问题