Creating stub for `private static readonly` field

橙三吉。 提交于 2020-01-04 07:35:17

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/45152652/creating-stub-for-private-static-readonly-field

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!