lundi 24 août 2015

Unit Testing Dependencies

Sometimes it seems that creating a test double ("Mock", "Stub", etc) of a class makes sense. It isolates the unit that needs to be tested and filters out irrelevant noise.

However, what happens when I need a test double that has to actually do the work? In that case, how should I unit test close dependencies?

Suppose I have a class like such:

public class StringFunctions()
{
    public string CapitalizeString(string str)
    {
        // return a string of letters that all need to be capitalized.
    }
}

And I have a class that uses StringFunctions as a dependency:

public class MyStringFunctions()
{
    private StringFunctions _strFuncs;

    public MyStringFunctions(StringFunctions strFuncs)
    {
        _strFuncs = strFuncs;
    }

    public string Put3AtEndOfStringAndCapitalize(string str)
    {
        return _strFuncs + 3;
    }
}

So now I would like to Test the method Put3AtEndOfStringAndCapitalize

Based off of the spec, I would like to give the method some conditions as such and I would expect:

cat => CAT3
THING => THING3
TEST3 => TEST33

In this case, how would I unit test MyStringFunctions using a "Test Double" of StringFunctions? The issue I'm having is that the "Test Double" needs an actual working CapitalizeString method.In this case, would it be best to not do any mocking?

Aucun commentaire:

Enregistrer un commentaire