dimanche 2 août 2015

Correct way to to TDD on a file reading word builder

I'm trying to get a handle on TDD. I'm creating a class which reads characters from a file, building words character by character.

The part I'm having a little trouble with is the method which builds the word and returns it.

public string GetNextWord()
{
    // please ignore implementation of characterReader,
    // I'm just using it as an example, I believe implementation is
    // irrelevant when unit testing, correct me if wrong
    return characterReader.NextWord();
}

How would I do this in a TDD manner? I've currently got a test file with known content. I pass that in to be read and check the words being returned match what's in the input file in the correct order.

Text file content: Test file content one End.

Unit test

string word1 = wordBuilder.GetNextWord();
string word2 = wordBuilder.GetNextWord();
.
.
Assert.IsTrue(String.Equals(word1, "Test");
.
.

Is this correct? I don't feel 'right' about having the test depend on an external input file and checking against hard coded strings. Could someone expound the correct TDD way of doing this please? :)

Aucun commentaire:

Enregistrer un commentaire