I'm trying to write unit tests for business logic classes I have control on, but which operates over some services that are not designed with the testability in mind. Currently I’ve extracted the problematic services as constructor injected ones, but I still need to mock them during the arrange part of the tests. The approach I intend to take is to wrap every non controlled service and to delegate the actual work to the wrapped service. I'm considering to:
- Option A. to implement a Facade and to expose only the subset of the service API that's in use, delegating the actual implementation to the aggregated service.
- Option B. to implement a Decorator, just for the sole purpose to provide an interface later to mock on.
Is there a better approach? Which of the two makes more sense? Pros and cons?
Thank you in advance!
Option A
public class OriginalService
{
public int Property1 { get; set; }
public bool DoSomething1(int param1) { ... }
}
interface IWrapperService
{
int Property1 { get; set; }
bool DoSomething1(int param1);
}
public class WrapperService : IWrapperService
{
private readonly OriginalService _service;
public WrapperService(OriginalService service)
{
_service = service;
}
public int Property1
{
get { return _service.Property1; }
set { _service.Property1 = value; }
}
public bool DoSomething1(int param1)
{
return _service.DoSomething1(param1) ;
}
}
Option B
public class OriginalService
{
public int Property1 { get; set; }
public bool DoSomething1(int param1) { ... }
}
interface IWrapperService
{
int Property1 { get; set; }
bool DoSomething1(int param1);
}
public class WrapperService : OriginalService, IWrapperService { ... }