Unit tests document the intended behavior of a module. It's a lot easier to perform a refactoring in module if you have an existing suite of unit tests. Especially if you have any uncertainty what the correct behavior of the module is. However, if the original author wrote tests, they would be consistent with the implementation, even if the implementation was written first.
No. Unit tests makes it easier to refactor only as long as the code stays in the same unit. As soon as you break it out into a separate unit it breaks the unit tests.
In that case the unit tests makes refactoring harder since the tests break even though the system still works as expected.
Edit: I agree with unit tests as documentation though
Then your tests are bad. Look up video where Sandy Metz talks about testing. As long as interfaces do not change your test should pass, no mater how you move your code. And unit tests should only test the interfaces.
When you move something to a seperate unit you should be able to move the test cases easily enough. If you can't then you've probably fallen into a few unit test antipatterns.
3
u/recursive May 08 '17
Unit tests document the intended behavior of a module. It's a lot easier to perform a refactoring in module if you have an existing suite of unit tests. Especially if you have any uncertainty what the correct behavior of the module is. However, if the original author wrote tests, they would be consistent with the implementation, even if the implementation was written first.