How to make sure that code is still working after refactoring ( i.e, after variable name change)?
In static language, if a class is renamed but other referring class is not, then I will get a compilation error.  
But in dynamic language there is no such safety net, and your code can break during refactoring if you are not careful enough. You can use unit test,  but when you are using mocks it's pretty hard to know the name changes and as a consequence, it may not help.
How to solve this problem?
1) For Python use PyUnit for PHP phpunit.
2) TDD approach is good but also making tests after writing code is acceptable.
3) Also use refactoring tools that are available for Your IDE they do only safe refactorings.
In Python You have rope (this is library but have plugins for most IDEs).
4) Good books are:
'Test-Driven Development by example' Best
'Expert Python Programing' Tarek Ziade (explain both TDD and refactoring)
google tdd and database to find a good book about TDD approach for developing databases.
Add info for mocks you are using. AFAIK mocks are needed only when database or network is involved. But normally unit test should cover small pice of code (one class only) sometimes two classes so no mockup needed !!
How to Make sure the code is still working after refactoring ( Dynamic language)