I need to implement fakes for unit testing one of my methods. Problem is the method I need to test calls a class method and retrieves some system parameters.Scenario is as below:

 Class A(){
       public void method xx(){
       //This needs to be tested.
       //This method makes a call to retrieve some informations. The call is like 
             below:
             String culture=Api.GetEnvironmentData().GetCulture();
             //This is the problem area.
             boolean implmentApi=Api.GetEnvironmentData().DoImplmentApi();
             //This is the problem area.
      }
    }

This GetEnvironmentData method is something like this:

public static EnvironmentData GetEnvironmentData ()
 {
   return GetDiContainer().Resolve<EnvironmentData >();                   
 }

EnvironmentData class is something like this:

public class EnvironmentData(){
 public EnvironmentData(IEnvironmentDataProvider EnvironmentDataProvider){
//
 }
}

I can fake the IEnvironmentDataProvider using moq but am not able to figure out how to fake the EnvironmentData class. I need to fake the EnvironmentData class because it manipulated the results of IEnvironmentDataProvider based of various method calls. For example both GetCulture and DoImplmentApi call the getData method of the interface IEnvironmentDataProvider and then cast them accordingly. Now when I fake the IEnvironmentDataProvider and return some value I am not able to control what to return when GetCulture and when DoImplmentApi is called.

Can some one suggest how to implement the fakes for the above scenario.

2

There are 2 answers

3
Pawel Maga On

You don't need to mock dependencies of EnvironmentData class. I see one problem here: you are using DI container, like Service Locator, which in this scenario behave like antipattern. All dependencies should be injected, e. g.: by constructor or property.

Change your Api class to something like this:

 public class Api
 {
       private readonly EnvironmentData _environmentData;

       public Api(EnvironmentData envData)
       {
            environmentData = envData;
       }

       public string GetCulture()
       {
            return _envData.GetCulture();                   
       }
 }

Remember that implementation details of Api class should be hidden. You shouldn't expose EnvironmentData in this scenario. Api class should ask for all dependencies which are needed to implement this class and has own interface.

1
andreasnico On

Based on your comments, I think your best shot is to wrap the static class in a facade. Then you can mock the facade.

Something like this:

Class A
{
       IEnvironmentDataFacade _environmentDataFacade;
       Class A(IEnvironmentDataFacade environmentDataFacade)
       {
            _environmentDataFacade = environmentDataFacade;
       }

       public void method xx()
       {
            //Now you can fake IEnvironmentDataFacade:
             String culture= _environmentDataFacade.GetCulture();
             //Do the same as above with the method here:
             boolean implmentApi=Api.GetEnvironmentData().DoImplmentApi();
             //This is the problem area.
      }
}

public class EnvironmentDataFacade : IEnvironmentDataFacade
{
      public string GetCulture()
      {
            return Api.GetEnvironmentData().GetCulture();
      }


}

public interface IEnvironmentDataFacade
{
      string GetCulture();
}