在模拟 WebOperationContext 中执行 CreateTextResponse(...) 就像在 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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
CreateTextResponse
不是虚拟的,因此您无法使用最小起订量来模拟它。您可能想要围绕CreateTextResponse
创建一个包装器。您可以在单元测试期间模拟包装器,但在运行时委托给实际的WebOperationContext
。CreateTextResponse
is not virtual so you can't mock it with moq. You'll probably want to create a wrapper aroundCreateTextResponse
. You can mock the wrapper during unit testing, but delegate to the actualWebOperationContext
at runtime.您应该看一下 WCFMock,它有一个
IWebOperationContext
和WebOperationContextWrapper
。这些不包括CreateTextResponse
方法,但您可以将它们用作节省时间的起点。您还可能能够使用其他接口和包装器。You should take a look at WCFMock, which has an
IWebOperationContext
andWebOperationContextWrapper
. These don't include theCreateTextResponse
method, but you can use them as a starting point to save time. You'll also likely be able to make use of the other interfaces and wrappers.