Extract Method


I m going to extract the code that talks to the TextBox from the KeyDownHandler. I think I ll begin with just the last few lines that set the TextBox. I plan two steps, more for purposes of explaining than anything else. First, let s make a new method:

 public void PutText() { 
textbox.Lines = model.LinesArray();
textbox.SelectionStart = model.SelectionStart;
textbox.ScrollToCaret(); // keep cursor on screen. no test.
}

And we ll call it from the handler, replacing the last three lines. I removed the debugging stuff as well, so the new handler looks like this:

 void XMLKeyDownHandler(object objSender, KeyEventArgs kea) { 
model.SetLines(textbox.Lines);
model.SelectionStart = textbox.SelectionStart;
if (kea.KeyCode == Keys.Enter) {
model.Enter();
kea.Handled = true;
}
PutText();
}

Normally, I d just compile now and run the tests. But my tests don t test this! I ll have to test it manually. This is awful ! But it seems to work. Now for the second step.

The PutText method refers to the instance variable textbox. I want to pass it in as a parameter instead. This may seem odd at first, but remember that I want to use this thing with my Mock Object.

However, thinking about this tells me that I don t have a clear picture of what my test will look like, so I m speculating about what I want. And surely so are you. I should be writing the test first. So I ll do that now.




Extreme Programming Adventures in C#
Javaв„ў EE 5 Tutorial, The (3rd Edition)
ISBN: 735619492
EAN: 2147483647
Year: 2006
Pages: 291

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net