c# - How to write tests for legacy code and then refactor it? -


i have project lots of ugly code i've written time ago. i'm trying make testable , use tdd further development. every time i'm trying write unit tests existing code i'm getting stuck. don't know how test old code. can't write little test. makes me sick. can show me tests write function make further refactoring painless:

public void changehealth(uint16 id, healthtypes health, fightercount count) {     var uc = new fightercommand {health = health, killerid = 1024};     fighter f;     switch(count)     {         case fightercount.all:             if (fight.getinstance().status == fightstatuses.enable)             {                 foreach (var u in fighters.elements)                     uc.addunit(u.id);             }             fightevents.statusunit(null, health);             _sender.sendcommand_appserver(uc);             break;         case fightercount.notequal:             (var = fighters.count - 1; >= 0; i--)             {                 f = fighters.getunit(i);                 if (health == f.health) continue;                 uc.addunit(f.id);                 fightevents.statusunit(f, health);             }             if (uc.unitcount > 0) _sender.sendcommand_appserver(uc);             break;         default:             f = fighters.getbyid(id);             uc.addunit(f.id);             fightevents.statusunit(f, health);             _sender.sendcommand_appserver(uc);             break;     } } 

take @ "golden master" technique michael feathers. idea throw input @ untestable code, record output or state of program. can refactor , throw same input , observe same output or internal state (with exception of minor incremental change you've made). bit bit code becomes more testable. explained in further depth @ http://blog.thecodewhisperer.com/2014/09/28/surviving-legacy-code-with-golden-master-and-sampling/

a tool dumping state of application https://github.com/kbilsted/stateprinter can say

var printer = new stateprinter(); console.writeline( printer.printobject( myprogram) ); 

Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -