概述
public abstract class MirAPIUnitTestCommon { public abstract string GetBaseAddress(); /// <summary> /// CRUD /// </summary> /// <typeparam name="TResult"></typeparam> /// <param name="api"></param> /// <returns></returns> protected TResult InvokeRequest<TResult, TArguemnt>(string api, HttpMethod httpMethod, TArguemnt arg) { using (var invoker = CreateMessageInvoker()) { using (var cts = new CancellationTokenSource()) { var request = new HttpRequestMessage(httpMethod, api);if (null != arg) { request.Content = new ObjectContent<TArguemnt>(arg, new JsonMediaTypeFormatter()); } HttpClient httpClient = new HttpClient(); using (HttpResponseMessage response = httpClient.SendAsync(request, cts.Token).Result) { string result = response.Content.ReadAsStringAsync().Result; return JsonConvert.DeserializeObject<TResult>(result); } } } } private HttpMessageInvoker CreateMessageInvoker() { var config = new HttpConfiguration(); WebApiConfig.Register(config); var server = new HttpServer(config); var messageInvoker = new HttpMessageInvoker(server); return messageInvoker; } }
转载于:https://www.cnblogs.com/mibing/p/8778226.html
最后
以上就是落寞猫咪为你收集整理的.NET webapi 的单元测试的全部内容,希望文章能够帮你解决.NET webapi 的单元测试所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复