Search code examples
c#unit-testingmockinginversion-of-controlioc-container

Creating stub for `private static readonly` field


Due on Improper Instantiation problem it is recommended to create private static readonly instance of HttpClient.

Due on lack of time I have injected mocked client into test method with client as their parameter.

The problem is how can I in simple way inject mock into private static readonly HttpClient field of SingleHttpClientInstanceController?


Solution

  • how can I in simple way inject mock into private static readonly HttpClient field of SingleHttpClientInstanceController?

    Answer: There is no simple way.

    Suggestion:

    Abstract the resource behind an accessor

    public interface IHttpClientAccessor {
        HttpClient HttpClient { get; }
    }
    

    and inject that into the dependent controller.

    public class SingleHttpClientInstanceController : ApiController {
        private readonly HttpClient HttpClient;
    
        public SingleHttpClientInstanceController(IHttpClientAccessor httpClientAccessor) {
            HttpClient = httpClientAccessor.HttpClient;
        }
    
        // This method uses the shared instance of HttpClient for every call to GetProductAsync.
        public async Task<Product> GetProductAsync(string id) {
            var hostName = HttpContext.Current.Request.Url.Host;
            var result = await HttpClient.GetStringAsync(string.Format("http://{0}:8080/api/...", hostName));
            return new Product { Name = result };
        }
    }
    

    The same should also be done for accessing HttpContext which is what was recently introduced in Asp.Net-Core's IHttpContextAccessor

    An implementation of the IHttpClientAcessor can look something like this

    public class HttpClientAccessor : IHttpClientAccessor {
        static readonly Lazy<HttpClient> client = new Lazy<HttpClient>(() => new HttpClient());
        public HttpClient HttpClient { get { return client.Value; } }
    }
    

    So now for tests you can inject mock of the dependency.

    If using a DI container remember to register the accessor as a singleton as well.