在WebOperationContext中模拟WebOperationContext中执行CreateTextResponse(...)(Performing CreateTextResponse(…

编程入门 行业动态 更新时间:2024-10-22 02:40:01
WebOperationContext中模拟WebOperationContext中执行CreateTextResponse(...)(Performing CreateTextResponse(…) in mocking WebOperationContext as in WebOperationContext)

我正在使用包装器模拟WebOperationContext类进行单元测试(使用Moq)。 但我需要在我的模拟上下文中从WebOperationContext类执行CreateTextResponse(...)方法以生成消息。 你能不能给我任何建议怎么做?

编辑 :下面是我用于WebOperationContext的当前模拟。 但是,我无法实现CreateTextResponse / CreateStreamResponse。

public IAsyncResult BeginGetData(AsyncCallback asyncCallback, object asyncState) public Message EndGetData(IAsyncResult asyncResult) public class OperationContextMock : IOperationContext { public HttpCookieCollection Cookies { get; set; } public Message CreateStreamResponse(Action<System.IO.Stream> streamWriter, string contentType) { throw new NotImplementedException(); } public Message CreateTextResponse(string text, string contentType) { // How to mock this method so that it returns a Message object? } public string LookupRequestParameter(RequestParameter requestParameter) { throw new NotImplementedException(); } public NameValueCollection QueryParameters { get; set; } public NameValueCollection RequestHeaders { get; set; } public Uri RequestUri { get; set; } public string ResponseContentType { get; set; } public string ResponseLocation { get; set; } public HttpStatusCode ResponseStatusCode { get; set; } public CatalogServiceOperationContextMock() { this.ResponseStatusCode = HttpStatusCode.OK; } }

I'm mocking the WebOperationContext class over wrapper for unit testing (using Moq). But I need to execute CreateTextResponse(...) method from WebOperationContext class in my mocked context for Message generation. Could you please give me any recommendation how to do that?

EDIT: Below is the current mock I am using for the WebOperationContext. However, I can't get to implement CreateTextResponse/CreateStreamResponse.

public IAsyncResult BeginGetData(AsyncCallback asyncCallback, object asyncState) public Message EndGetData(IAsyncResult asyncResult) public class OperationContextMock : IOperationContext { public HttpCookieCollection Cookies { get; set; } public Message CreateStreamResponse(Action<System.IO.Stream> streamWriter, string contentType) { throw new NotImplementedException(); } public Message CreateTextResponse(string text, string contentType) { // How to mock this method so that it returns a Message object? } public string LookupRequestParameter(RequestParameter requestParameter) { throw new NotImplementedException(); } public NameValueCollection QueryParameters { get; set; } public NameValueCollection RequestHeaders { get; set; } public Uri RequestUri { get; set; } public string ResponseContentType { get; set; } public string ResponseLocation { get; set; } public HttpStatusCode ResponseStatusCode { get; set; } public CatalogServiceOperationContextMock() { this.ResponseStatusCode = HttpStatusCode.OK; } }

最满意答案

CreateTextResponse不是虚拟的,所以你不能用moq模拟它。 您可能想要围绕CreateTextResponse创建一个包装器。 您可以在单元测试期间模拟包装器,但在运行时委托给实际的WebOperationContext 。

CreateTextResponse is not virtual so you can't mock it with moq. You'll probably want to create a wrapper around CreateTextResponse. You can mock the wrapper during unit testing, but delegate to the actual WebOperationContext at runtime.

更多推荐

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

发布评论

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

>www.elefans.com

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