mardi 7 juillet 2015

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

I have a project with lots of ugly code I've written some time ago. Now I'm trying to make it testable and to use TDD for further development. But every time I'm trying to write unit tests for existing code I'm getting stuck. I just don't know how to test my old code. I can't write any little test. It makes me sick. So can anyone show me what tests to write for function like this to 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:
            for (var i = Fighters.Count - 1; i >= 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;
    }
}

Aucun commentaire:

Enregistrer un commentaire