2

Using C# and Visual Studio, I've developed a class that zips and archives a folder into desired location, creating directories and manipulating files on a machine.

The intended use is as an part of an embedded piece of software that will collect logging information for another piece of software after its completion.

This class is setup from parent classes that will pass it known configuration information, this class does not care where it came from it just needs to be there. The class requires directory and file information passed to it which is derived from the config files.

I know what needs to be tested but what I am struggling with is how I should write automated tests if that is even possible. The reason this is such a struggle is that the configuration information is most certainly going to be different for each user.

Are automated tests even possible without developer having to modify them every time we need to run tests? How would you approach this or am I overthinking this?

2

If you encapsulate all IO operations in a small class with an interface and inject it to the archiving class you can mock calls to the IO system during testing.

public class Archiver
{
    public IFileSystem Filesystem { private get; set; }

    public void DoWork() 
    {
        //business logic here
    }

    public Archiver() { Filesystem = new RealFilesystemClass(); }
}


[TestClass]
class ArchiveUnitTests
{
    Archiver archiver = new Archiver { Filesystem = new MockFilesystem() };

    [Test]
    public void Reads_From_Filesystem()
    {
        //Test code here
    }
}
  • 2
    I think constructor injection is more appropriate here. You could have two constructors: 1) A parameterless constructor that instantiates RealFileSystemClass and 2) A second constructor accepting an IFileSystem object as its argument. This gives you an intelligent default, and a test harness to facilitate unit testing the Archiver class. – Greg Burghardt Apr 25 '16 at 18:05
  • 2
    A big chunk of the C# libraries have already been wrapped for this effect github.com/jozefizso/SystemWrapper – Sign Apr 25 '16 at 18:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.