How can I use unit testing when classes depend on each other or external data?

I would like to start using unit tests, but it's hard for me to understand how I can use them in my current project.

My current project is an application that collects files into a "Directory". A Catalogcan then extract information from files that it contains, such as thumbnails and other properties. Users can also tag files with other custom metadata, such as Author and Notes. It can be easily compared to a photo album application such as Picasa or Adobe Lightroom.

I separated the code to create and process Catalogin a separate DLL, which I now want to test. However, most of my classes should never be created on their own. Instead, everything happens through my class Catalog. For example, I can not check my class Filemyself, because it Fileis available only through Catalog.

As an alternative to unit testing, I believe that it would be more appropriate for me to write a test program that starts a series of actions, including creating a directory, reopening the created directory and managing the contents of the directory. See code below.

//NOTE: The real version would have code to log the results and any exceptions thrown

//input data
string testCatalogALocation = "C:\TestCatalogA"
string testCatalogBLocation = "C:\TestCatalogB"
string testFileLocation = "C:\testfile.jpg"
string testFileName = System.IO.Path.GetFileName(testFileLocation);


//Test creating catalogs
Catalog catAtemp = Catalog(testCatalogALocation)
Catalog catBtemp = Catalog(testCatalogBLocation );


//test opening catalogs
Catalog catA = Catalog.OpenCatalog(testCatalogALocation);
Catalog catB = Catalog.OpenCatalog(testCatalogBLocation );


using(FileStream fs = new FileStream(testFileLocation )
{
    //test importing a file
    catA.ImportFile(testFileName,fs);
}

//test retrieving a file
File testFile = catA.GetFile(System.IO.Path.GetFileName(testFileLocation));

//test copying between catalogs
catB.CopyFileTo(testFile);


//Clean Up after test
System.IO.Directory.Delete(testCatalogALocation);
System.IO.Directory.Delete(testCatalogBLocation);

-, - ? unit test ? -, , , , Visual Studio? " " VS2010 ?


. . , . , . .

. , , "" . , IFile, . , ICatalog, , .

, ICatalog, . - , /.

+5
5

, . , , .

  • , /, , , .

  • . , , .

  • , .

  • , , .

  • , . , "sensing" , , . , - , , . - , , / , , , .

  • , , , , . , , , , , , , . , , , , , , , .

. , . , , , ( , ), , .

+2

SOLID. , "D" SOLID / , , , , . IoC (Inversion of Control) (, Unity, Ninject Castle Windsor), , /stub .

, :

public class ComplexAlgorithm
{
    protected DatabaseAccessor _data;

    public ComplexAlgorithm(DatabaseAccessor dataAccessor)
    {
        _data = dataAccessor;
    }

    public int RunAlgorithm()
    {
        // RunAlgorithm needs to call methods from DatabaseAccessor
    }
}

RunAlgorithm() ( DatabaseAccessor), . DatabaseAccessor .

public class ComplexAlgorithm
{
    protected IDatabaseAccessor _data;

    public ComplexAlgorithm(IDatabaseAccessor dataAccessor)
    {
        _data = dataAccessor;
    }

    // rest of class (snip)
}

ComplexAlgorithm IDatabaseAccessor, , Unit test ComplexAlgorithm . :

public class MyFakeDataAccessor : IDatabaseAccessor
{
    public IList<Thing> GetThings()
    {
        // Return a fake/pretend list of things for testing
        return new List<Thing>()
        {
            new Thing("Thing 1"),
            new Thing("Thing 2"),
            new Thing("Thing 3"),
            new Thing("Thing 4")
        };
    }

    // Other methods (snip)
}

[Test]
public void Should_Return_8_With_Four_Things_In_Database()
{
    // Arrange
    IDatabaseAccessor fakeData = new MyFakeDataAccessor();
    ComplexAlgorithm algorithm = new ComplexAlgorithm(fakeData);
    int expectedValue = 8;

    // Act
    int actualValue = algorithm.RunAlgorithm();

    // Assert
    Assert.AreEqual(expectedValue, actualValue);
}

"" . - .

, , , SOLID , , Unit test .

+8
+2

D - D - TDD. , , . , unit test , , . .

; , , DI, . TDD , TDD : .

+1

Legacy Code. : , . Catalog, . , / .

, - , , , TDD .

0

All Articles