Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I am creating an application which will be testable(unit + integration). In this application I have a FileHelper static class,

public static class FileHelper
{
    public static void ExtractZipFile(Stream zipStream, string location)
    {
        ..................................
    } 
    public static void CreatePageFolderIfNotExist(string directory)
    {
        .................................................
    }

    .......................................................
    .......................................................
}

But due to static I think it is not testable. How to make this class testable?

share|improve this question
1  
Why would you want this class to be static at all? If you want testable code, avoid them. I'd change the API slightly. so you use Zip = new Zip('file.zip'); Zip.extractTo('/path/); Combining these seemingly unrelated functions is poor separation of concerns. –  Tom B Feb 7 '13 at 11:40
    
@Tom B, "if you want testable code, then avoid static classes "... Really? Seriously? So, if I have a function public static double sqrt(double x), then I just cannot test it? I cannot pass a few parameters in and make sure that I get the expected result or exception? If I have a more complicated function/method that affects the file system or creates some other side effect, is it suddenly that much different? –  Leonid Feb 7 '13 at 19:53
3  
@Leonid see: misko.hevery.com/2008/12/15/… It's not the static methods which are hard to test, it's the code which uses them. It becomes difficult to test because it's impossible to substitute them for mocks. You must test the client code and the static method during the test. This means it's impossible to know whether a bug is in the method you're actually testing or the static method it calls, which defeats the purpose of unit testing-- it's impossible to isolate the exact code you're trying to test. –  Tom B Feb 8 '13 at 9:28
    
I'm a big fan of static methods, but these should not be static since they access external state. –  CodesInChaos Feb 19 '13 at 15:32
1  
The tldr of static methods is that they should generally be stateless. Stateless methods are usually pretty easy to test, because you have a well-defined set of inputs and well-defined outputs. When testing a piece of code which uses stateless static methods, you simply treat them as part of the unit you are testing, no different than if the code is in-lined. –  Dan Lyons Sep 6 '13 at 17:16
add comment

1 Answer 1

up vote 8 down vote accepted

If you want to write tests for this helper class only - they would be integration tests since they touch the file system.

In order to make the code unit testable you need to make it independent from hardware and physical interaction with network or disk.

If you want to unit test the business logic that uses this FileHelper, then you should introduce Inversion of Control to separate business logic from file activities. You'll have to create an interface that describes all operations you can do with files, transform this static helper class into class implementing that interface, and reference file operations from business logic via interface only. Example of file helper:

public interface IFileOperations
{
    void ExtractZipFile(Stream zipStream, string location);
    void CreatePageFolderIfNotExist(string directory);
}

public class FileOperations : IFileOperations
{
    public void ExtractZipFile(Stream zipStream, string location)
    {
        ..................................
    } 

    public void CreatePageFolderIfNotExist(string directory)
    {
        .................................................
    }
}

Then, in order to properly cover you code with tests, you would need to write unit tests for business logic and add integration tests for the implementation of FileOperations.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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