2

データを書き込むパブリック関数を書いていますHttpContext.Current.Response.AppendToLogが、このためのユニットテストを書く方法はありますか?

AppendToLogによって書き込まれたデータをアサートする方法のモック/カスタムオブジェクトを作成HttpContextして割り当ててみました。HttpContext.Current

4

1 に答える 1

3

インターフェイスを作成し、実装で HttpContext をラップします。

public interface IHttpContext
{
    void AppendToResponseLog(/*parmas go here*/);
}

public class HttpContextWrapper : IHttpContext
{
    private HttpContext _httpContext = HttpContext.Current; //or constructor param


    public void AppendToResponseLog(/*parmas go here*/)
    {
        _httpContext.Response.AppendToLog(/*params*/);
    }  
}

IHttpContextクラスをの代わりに に依存させますHttpContext.Current。テストに関しては、モックできるようになりIHttpContextました。

注: テスト/モックできるようにするすべての .NET Framework 依存関係に対して、この同じ方法を使用します。

于 2012-11-14T15:38:47.343 に答える