views:

81

answers:

1

Hello,

I have the following code:

private static void WriteStartupInfo()
    {
        Settings settings = Settings.Default;
        TimeSpan interval = settings.CleanupInterval;
        TimeSpan span = settings.ExpiryTimeSpan;
        string url = settings.TeamFoundationServerUrl; 
        StringBuilder sb = new StringBuilder();
        sb.Append(String.Format("CleanupInterval:{0}{1}", interval,Environment.NewLine));
        sb.Append(String.Format("ExpiryTimeSpan:{0}{1}", span,Environment.NewLine));
        sb.Append(String.Format("TeamFoundationServerUrl:{0}{1}", url,Environment.NewLine));
        Trace.TraceInformation(sb.ToString());
    }

This needs to be tested. How do I unit test this ?

Is it as simple as removing trace listeners and adding a unit test trace listener ?

Any help will be greatly appreciated.

+2  A: 

It depends on what you're testing. You could break the method up into several methods so that you could test the important functionality. For example, if you want to test the string that is built, pull that into a separate function!

private static void WriteStartupInfo()
{
    Trace.TraceInformation(GetStartupInfoString());
}

private static String GetStartupInfoString() {
    Settings settings = Settings.Default;
    TimeSpan interval = settings.CleanupInterval;
    TimeSpan span = settings.ExpiryTimeSpan;
    string url = settings.TeamFoundationServerUrl; 
    StringBuilder sb = new StringBuilder();
    sb.Append(String.Format("CleanupInterval:{0}{1}", interval,Environment.NewLine));
    sb.Append(String.Format("ExpiryTimeSpan:{0}{1}", span,Environment.NewLine));
    sb.Append(String.Format("TeamFoundationServerUrl:{0}{1}", url,Environment.NewLine));
    return sb.ToString();
}

Now the string can be tested independently - I would argue that the WriteStartupInfo() function is trivial at that point, and you don't need to test the Trace functionality.

Note that you may need to fiddle with the scope of the methods - I haven't worked with testing private methods, so I can't help there.

Chris Marasti-Georg
Okay, that looks nice. Thanks.
KeesDijk