Imagine: I want to create new functionality: Scan a file for virus.
Using TDD i want to make the following tests:
-
ScanningAnInfectedFileReturnsFalse
-
ScanningACleanFileReturnsTrue
In code i want to create this function:
/// <summary>
///
/// </summary>
/// <param name="fullFileName">The File To Scan</param>
/// <returns>True: File is clean, False: File contains virus</returns>
public static bool ScanFile(string fullFileName)
{
//TODO: Scan the given FullFileName
throw new NotImplementedException();
}
My Question: How can i implement the two tests using the given function? I think the function should be refactored to make it testable?
The problem you have is that this method
ScanFileby taking a path to the file, has too many responsibilities which makes it hard to test.*With the item annotated
*being really all that this method should do. In this way your method would be far more testable.One possible way to implement this change is to have the method take an interface:
Then, say your scan method is looking for specific sequences of bytes the interface could be as simple as:
Your testing could then simply mock this interface (See Rhino, NMock etc) for your 2 tests, one which returns a bad sequence of bytes, one which returns good. Your real implementation would be as simple as having a concrete class: