views:

516

answers:

2

I am writing a number of unit tests for a logger class I created and I want to simulate the file class. I can't find the interface that I need to use to create the MOQ... so how do you successfully MOQ a class without an interface?

It also isn't clear to me how I can use dependency injection without having an interface available:

private FileInfo _logFile;

public LogEventProcessorTextFile(FileInfo logFile) {
    _logFile = logFile;
}

When I really want to do something like this (note IFileInfo instead of FileInfo):

private IFileInfo _logFile;

public LogEventProcessorTextFile(IFileInfo logFile) {
    _logFile = logFile;
}
+9  A: 

Design your code so that instead of accessing the FileInfo class directly, access an interface (named for example IFileInfo) with the same capabilities. In production code you will use a class that just delegates all its functionality to the system FileInfo class, but for unit testing you can mock the interface.

For example, in an application I made that acted differently depending on the current date, I declared the following interface:

interface IDateTimeProvider
{
    DateTime Today();
}

And the production class was just:

class DateTimeProvider : IDateTimeProvider
{
    public DateTime Today()
    {
        return DateTime.Today;
    }
}

You can complement this approach with the usage of a dependency injection engine to decide whether a real class or a mock should be used in each case.

Konamiman
I'm definitely 100% for the idea of use interfaces and using dependency injection, but my issue was not being able to find the interface for FileInfo... are you saying that I should create an interface that matches my usage of FileInfo? If so how can I have FileInfo 'inherit' (sorry if that's incorrect terminology usage when applied to what I'm asking) my interface without having access to its source?
CuriousCoder
You wouldn't have FileInfo inherit your new interface. Instead, you wrap FileInfo in your own class that does implement the interface. For more info, see the section "Wrap infrastructure that cannot be mocked" from this page: http://www.lostechies.com/blogs/gabrielschenker/archive/2009/02/27/refactoring-legacy-code.aspx
Pedro
Thank you very much for your answer... it helped me understand the 'gateway pattern' which is clearly the way to handle the situation in my question. If you aren't familar with the SystemWrapper library that wcoenen pointed me in the direction of... you should definitely check it out... it turns out to be exactly what I needed. But thank your for your input it was incredibly valuable to me.
CuriousCoder
Of course you must always accept the answer that best solves your problem. Anyway how come that there isn't a badge for "first answer accepted and then un-accepted"? :-)
Konamiman
+5  A: 

Use SystemWrapper, a library which provides interfaces and mockable wrappers classes for many .NET classes which don't implement interfaces themselves.

Wim Coenen
I have started using SystemWrapper now and it's a fantastic library. Thank you very much!
CuriousCoder
Nice link, thanks for sharing! I could probably use this concept.
Grant Palin