4

I am working on a brownfield application and am currently refactoring part of it. I am trying to do this in a TDD fashion but am running into a problem. Part of the code I am testing does

var siteLanguages = from sl in SiteSettings.GetEnabledSiteLanguages() select sl.LanguageID;

where GetEnabledLanguages has the following signature

public static List<LanguageBranch> GetEnabledSiteLanguages();

it in turns calls data access code to retrieve the relevant information. Up untill now I have used a interface and DI to use a different stub implementation for these kind of dependencies during unit testing. But since the GetEnabledSiteLanguages method is static this will not work. What is the "correct" way to do it in this case?

Sam Holder
  • 32,535
  • 13
  • 101
  • 181
olle
  • 4,597
  • 24
  • 28
  • 1
    If you have the freedom to change the code, wrap the static method call in an adapter that implements an interface. Pass it in to the client. Now fake the interface implementation. ELSE you can look at other libs like Moles which can intercept calls to static methods.. – Gishu Sep 20 '11 at 03:24

4 Answers4

4

you could create a object which implements an interface and inject an implementation of this into the class which uses the SiteSettings class. The interface declare the method with the same signature as the static method(s) you need to intercept. Then you could mock out the interface for tests and create a single implementation which delegates to the static method for the actual code:

public interface ISiteSettings
{
    public List<LanguageBranch> GetEnabledSiteLanguages()
}

public class ActualSiteSettings : ISiteSettings
{
    public List<LanguageBranch> GetEnabledSiteLanguages()
    {
         return SiteSettings.GetEnabledSiteLanguages();
    }
}

... in the dependent class:

public class DependentClass
{
    private ISiteSettings m_siteSettings;

    public DependentClass(ISiteSettings siteSettings)
    {
    m_siteSettings=siteSettings;
    }

    public void SomeMethod
    {
         var siteLanguages = from sl in m_siteSettings.GetEnabledSiteLanguages() select sl.LanguageID;
    }
}
Sam Holder
  • 32,535
  • 13
  • 101
  • 181
  • I used to not like this approach because I didn't want to have to create an instance of the class where one wasn't needed before. But I've since learned about the [Singleton pattern](https://en.wikipedia.org/wiki/Singleton_pattern), which is useful for these cases. You could make `ActualSiteSettings` a singleton, for example. – inejwstine Aug 29 '19 at 19:32
2

What about making your method such as:

public static Func<List<LanguageBranch>> GetEnabledSiteLanguages = () => {
   //your code here
};

Now it becomes first class object (as Func delegate) and a stub can replace it

Ankur
  • 33,367
  • 2
  • 46
  • 72
0

You can use tools like JustMock, TypeMock or moles. These tools allow you to mock everythings like static methods.

meziantou
  • 20,589
  • 7
  • 64
  • 83
0

Look at Moles framework.

Kirill Polishchuk
  • 54,804
  • 11
  • 122
  • 125