Assuming you're using source control, here's what I'd do:
- Write the tests for the old code.
- Make sure the tests pass.
- Check that in to source control.
- Now, as a
separate change, replace the
implementation (ie: add new
implementation and delete old
implementation) and make sure the
tests still pass.
As part of step 1 you may want to write a little "harness" for the old code so you can see what values it spits out if your primary goal is just to preserve the existing behavior. The important thing is that you have tests that pass on the old implementation.
The reason for step 3 is that if you ever discover that you missed an important case you can sync to the change where you submitted the tests against the old code, add the new test, verify that it works, and then resync to head and verify that it still works (fixing the new implementation if necessary).