ShimNotImplementedException:System.Net.Http.HttpMessageInvoker.Dispose()(ShimNotImplementedException

编程入门 行业动态 更新时间:2024-10-27 03:33:18
ShimNotImplementedException:System.Net.Http.HttpMessageInvoker.Dispose()(ShimNotImplementedException : System.Net.Http.HttpMessageInvoker.Dispose())

我正在使用一个垫片(由Visual Studio生成)伪造一个HttpClient实例。 下面是SUT:

public class ExampleForSO { private HttpClient _httpClient; public ExampleForSO(HttpClient httpClient) { _httpClient = httpClient; } public async Task<List<string>> GetListAsync() { using (_httpClient) { return await GetListFromHostAsync(_httpClient); } } public async Task<List<string>> GetListFromHostAsync(HttpClient httpClient) { var responseJson = await httpClient.GetAsync("abc") .Result.Content.ReadAsStringAsync() ; var fList = JsonConvert.DeserializeObject<List<string>>(responseJson); return fList; }

这是单元测试:

[TestMethod] public void TestMethod1() { // Create a fake HttpClient using (ShimsContext.Create()) { var fake = new ShimHttpClient() { GetAsyncString = (uri) => { var hrm = new HttpResponseMessage(HttpStatusCode.OK); var list = new List<string>() {"file1", "file2"}; hrm.Content = new StringContent(JsonConvert.SerializeObject(list)); var t = new Task<HttpResponseMessage>(() => hrm); return Task.Run(() => hrm); } }; var _exampleForSO = new ExampleForSO(fake); var returnValue = _exampleForSO.GetListAsync(); // This line will throw an exception: var value = returnValue.Result; Assert.IsNotNull(returnValue); } }

当我运行它时,shim会抛出一个ShimNotImplemented异常。 堆栈跟踪显示:

$Action1NotImplemented71f6f23c-d227-4cb6-ac76-1f8c8094c413.Invoke(HttpMessageInvoker arg1) at System.Net.Http.HttpMessageInvoker.Dispose()

如果我using (_httpClient)注释掉行,单元测试将在不抛出异常的情况下运行,但这对于生产来说不是一个好的解决方案。 我的问题,如何将函数分配给System.Net.Http.HttpMessageInvoker.Dispose()shim委托? 我无法在任何地方找到它的引用。 我是否必须为HttpMessageInvoker创建一个假的并分配给另一个垫片属性?

I am using a shim (generated by Visual Studio) to fake an HttpClient instance. Below is SUT:

public class ExampleForSO { private HttpClient _httpClient; public ExampleForSO(HttpClient httpClient) { _httpClient = httpClient; } public async Task<List<string>> GetListAsync() { using (_httpClient) { return await GetListFromHostAsync(_httpClient); } } public async Task<List<string>> GetListFromHostAsync(HttpClient httpClient) { var responseJson = await httpClient.GetAsync("abc") .Result.Content.ReadAsStringAsync() ; var fList = JsonConvert.DeserializeObject<List<string>>(responseJson); return fList; }

Here is the unit test:

[TestMethod] public void TestMethod1() { // Create a fake HttpClient using (ShimsContext.Create()) { var fake = new ShimHttpClient() { GetAsyncString = (uri) => { var hrm = new HttpResponseMessage(HttpStatusCode.OK); var list = new List<string>() {"file1", "file2"}; hrm.Content = new StringContent(JsonConvert.SerializeObject(list)); var t = new Task<HttpResponseMessage>(() => hrm); return Task.Run(() => hrm); } }; var _exampleForSO = new ExampleForSO(fake); var returnValue = _exampleForSO.GetListAsync(); // This line will throw an exception: var value = returnValue.Result; Assert.IsNotNull(returnValue); } }

When I run this the shim throws a ShimNotImplemented exception. The stack trace shows this:

$Action1NotImplemented71f6f23c-d227-4cb6-ac76-1f8c8094c413.Invoke(HttpMessageInvoker arg1) at System.Net.Http.HttpMessageInvoker.Dispose()

If I comment out the line using (_httpClient), the unit test will run without throwing the exception, but that is not a good solution for production. My question, how can I assign a function to the System.Net.Http.HttpMessageInvoker.Dispose() shim delegate? I am not able to find a reference to it anywhere. Do I have to create a fake for the HttpMessageInvoker to and assign to another shim property?

最满意答案

你试过这个: Visual Studio 2013 Update 4改变MSFakes Shim对象默认行为基本上,调用Microsoft.QualityTools.Testing.Fakes.Shims.ShimBehaviors.BehaveAsDefaultValue(); 在实例化垫片之前

did you try this: Visual Studio 2013 Update 4 Changes MSFakes Shim Object Default Behaviour basically, call Microsoft.QualityTools.Testing.Fakes.Shims.ShimBehaviors.BehaveAsDefaultValue(); before instantiating the shim

更多推荐

本文发布于:2023-07-05 08:53:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1035446.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Net   System   ShimNotImplementedException   Dispose   HttpMessageInvoker

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!