We Need Some Tests Here

We Need Some Tests Here!

I did a little simple refactoring just above, just some renaming and method extraction, and got away with it. I was running the tests, but I had forgotten that none of them tested the TextModel that I was working on. No excuse , sir: we should have written the tests first, but sometimes even we nod. We ve decided that these chapters won t be sanitized: you ll see us learning, making mistakes, and, we hope, ultimately producing some pretty nice code. In what follows , we get down to writing the tests we should have written before.

We re still working on our first story:

When I m typing a paragraph inside P tags and hit a return, create another P tag underneath this one, and put the cursor in between the new tags, in the right typing location.

We have this functionality working in the GUI and therefore in the TextModel, which drives the GUI. We should have written tests for the TextModel already, but we couldn t figure out how. Now we know enough to do it, and we sorely need to write those tests. We ll start with simple ones, and move forward.

Our first test just checks to make sure that a TextModel that hasn t actually done anything answers an empty array of strings. Our purpose with this test is mostly to check that we are correctly set up.

 [SetUp] public void CreateModel() { 
model = new TextModel();

[Test] public void TestNoLines() {
model.Lines = new String[0];
AssertEquals(0, model.Lines.Length);

Next we test that a TextModel given three lines, with no processing, returns three lines. Again, a pretty simple test. We like to go in tiny steps, especially because these tests usually run, giving us a little jolt of success. Even so, once in a while we get a surprise. In addition, remember that we re just learning C#. We hadn t worked much with arrays of strings and such, so we take extra small steps, knowing that we re likely to write code that doesn t work. Anyway, here s the no-processing test:

 [Test] public void TestNoProcessing() { 
model.Lines = new String[3] { "hi", "there", "chet"};
AssertEquals(3, model.Lines.Length);

OK, so far so good. Now we ll actually do some work. This test takes a TextModel with one line of content, hello world , and tells it to insert a paragraph tag. We expect to find three lines in total: the original, a blank line, and a paragraph tag pair. The cursor should be between the Ps of the P tag. That s 18 characters in: 11 for hello world + 2 for newline + 2 for blank line + 3 for *ltP>.

 [Test] public void TestOneEnter() { 
model.Lines = new String[1] {"hello world" };
model.SelectionStart = 5;
AssertEquals(3, model.Lines.Length);
AssertEquals(18, model.SelectionStart);

Because we re confident in the content, we re not checking it. A case could be made that we should be checking it. In a later test, we actually do that. As always, it s a judgment call what to test when you re testing after the fact. Testing before the fact usually gives us better tests. Try it both ways yourself and see what you discover.

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