4

単体テスト用にラッパーを介して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;
    }
}
4

2 に答える 2

3

CreateTextResponse仮想ではないため、moqでモックすることはできません。おそらく、のラッパーを作成することをお勧めしますCreateTextResponse。単体テスト中にラッパーをモックすることはできますが、WebOperationContext実行時に実際に委任することができます。

于 2011-10-15T18:21:20.220 に答える
0

とが含まれているWCFMockを確認する必要がありIWebOperationContextますWebOperationContextWrapper。これらにはCreateTextResponseメソッドは含まれていませんが、時間を節約するための開始点として使用できます。また、他のインターフェースやラッパーを利用できる可能性もあります。

于 2011-10-15T22:04:13.280 に答える