在模拟 WebOperationContext 中执行 CreateTextResponse(...) 就像在 WebOperationContext 中一样

发布于 2024-12-09 18:20:20 字数 1421 浏览 0 评论 0原文

我正在通过包装器模拟 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

琴流音 2024-12-16 18:20:20

CreateTextResponse 不是虚拟的,因此您无法使用最小起订量来模拟它。您可能想要围绕 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.

内心激荡 2024-12-16 18:20:20

您应该看一下 WCFMock,它有一个 IWebOperationContextWebOperationContextWrapper。这些不包括 CreateTextResponse 方法,但您可以将它们用作节省时间的起点。您还可能能够使用其他接口和包装器。

You should take a look at WCFMock, which has an IWebOperationContext and WebOperationContextWrapper. These don't include the CreateTextResponse 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文