Unit tests: constructor injection with fake objects: bad tests?


In The Art of Unit Testing, 2nd Ed., The author gives the following example to inject a chunk using constructor injection and a "fake object". The goal of the "fake object" is to inherit the interface and break dependencies so that you can test it unitarily.

I don't understand how these are examples of legitimate evidence. The call to LogAnalyzer.IsValidLogFileName() used at runtime would call some implementation of IExtentionManager.IsValid(), which would interrogate the string in some way before returning a bool or throwing an exception. So for me the important test here is: for IsValidLogFileName()Does the method return what you expect when you give it a specific string?

In the examples below, the author encodes and establishes myFakeManager.WillBeValid within the tests for truepass myFakeManager to the LogAnalyzer and immediately states that what they have just established myFakeManager.WillBeValid to is true… What did they literally put it to ?!

How are these tests in LogAnalyzerTests.cs Useful?

LogAnalyzer.cs

public class LogAnalyzer
{
    private IExtensionManager manager;
    public LogAnalyzer(IExtensionManager mgr)
    {
        manager = mgr;
    }

    public bool IsValidLogFileName(string fileName)
    {
        return manager.IsValid(fileName);
    }
}

IExtensionManager.cs

public interface IExtensionManager
{
    bool IsValid(string fileName);
}

FakeExtensionManager.cs

internal class FakeExtensionManager : IExtensionManager
{
    public bool WillBeValid = false;
    public Exception WillThrow = null;

    public bool IsValid(string fileName)
    {
        if (WillThrow != null)
            throw WillThrow;

        return WillBeValid;
    }
}

LogAnalyzerTests.cs

(Test)
public void IsValidFileName_NameSupportedExtension_ReturnsTrue()
{
    FakeExtensionManager myFakeManager = new FakeExtensionManager();
    myFakeManager.WillBeValid = true;

    LogAnalyzer log = new LogAnalyzer(myFakeManager);

    bool result = log.IsValidLogFileName("short.ext");
    Assert.True(result);
}

(Test)
public void IsValidFileName_ExtManagerThrowsException_ReturnsFalse()
{
    FakeExtensionManager myFakeManager = new FakeExtensionManager();
    myFakeManager.WillThrow = new Exception("this is fake");

    LogAnalyzer log = new LogAnalyzerConstructor(myFakeManager);
    bool result = log.IsValidLogFileName("anything.anyextension");
    Assert.False(result);
}