我在包装器上模拟 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;
}
}
最佳答案
CreateTextResponse
不是虚拟的,因此您不能使用最小起订量模拟它。您可能希望围绕 CreateTextResponse
创建一个包装器。您可以在单元测试期间模拟包装器,但在运行时委托(delegate)给实际的 WebOperationContext
。
关于c# - 像在 WebOperationContext 中一样在模拟 WebOperationContext 中执行 CreateTextResponse(...),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7778354/