【问题标题】:Nunit tests on Response.Cache.VaryByHeaderResponse.Cache.VaryByHeader 上的 Nunit 测试
【发布时间】:2011-08-19 17:00:02
【问题描述】:

我正在使用 NUnit 和 NSubstitute 对使用 HttpResponse 的函数进行一些单元测试,我知道您无法模拟这些对象,因此我创建了接口来表示它们以及其中的一些属性。

我无法理解如何为 Response.Cache.VaryByHeader 创建接口

// This is my HttpResponse interface 
public interface IHttpResponse
{
    Stream Filter { get ; set; }
    IHttpCachePolicy Cache { get; set; }
    void AppendHeader(string name, string value);
}   

// concrete httresponse 
public class HttpResponseProxy : IHttpResponse
{
    private HttpResponse _httpResponse;

    public Stream Filter {
        get {
            return _httpResponse.Filter ?? new MemoryStream();
        }
        set { _httpResponse.Filter = value; }
    }

    public IHttpCachePolicy Cache
    {
        get { return new HttpCachePolicyProxy(_httpResponse.Cache); }
        set { }
    }

    public HttpResponseProxy(HttpResponse httpResponse)
    {
        if (httpResponse == null)
        {
            throw new ArgumentNullException("httpResponse");
        }

        _httpResponse = httpResponse;
        _httpResponse.Filter = httpResponse.Filter;
    }

    public void AppendHeader(string name, string value)
    {
        _httpResponse.AppendHeader(name, value);
    }
}    

// HttpCachePolicy interface 
public interface IHttpCachePolicy
{
    IHttpCacheVaryByHeaders VaryByHeaders { get; set; }
}

// concrete HttpCachePolicy
public class HttpCachePolicyProxy : IHttpCachePolicy
{
    private HttpCachePolicy _httpCachePolicy;
    public HttpCachePolicyProxy(HttpCachePolicy httpCachePolicy)
    {
        _httpCachePolicy = httpCachePolicy;
    }

    public IHttpCacheVaryByHeaders VaryByHeaders
    {
        get { return new HttpCacheVaryByHeadersProxy(_httpCachePolicy.VaryByHeaders as HttpCacheVaryByHeaders); }
        set {  }
    }
}


public interface IHttpCacheVaryByHeaders
{
    IHttpCacheVaryByHeaders HttpCacheVaryByHeaders { get; set; } 
}

public class HttpCacheVaryByHeadersProxy : IHttpCacheVaryByHeaders
{
    private HttpCacheVaryByHeaders _httpCacheVaryByHeaders;
    public HttpCacheVaryByHeadersProxy(HttpCacheVaryByHeaders httpCacheVaryByHeaders)
    {
        _httpCacheVaryByHeaders = httpCacheVaryByHeaders;
    }

    public IHttpCacheVaryByHeaders HttpCacheVaryByHeaders
    {
        get { return new HttpCacheVaryByHeadersProxy(_httpCacheVaryByHeaders); }
        set {  }
    }
}

这是我实际测试的功能:

public static void CompressPage(IHttpRequestGetCompressionMode getCompressionMode, IHttpResponse httpResponse)
    {
        string supportedCompression = getCompressionMode.GetClientSupportedCompressionMode();
        if (supportedCompression != HttpHeaderValues.NoCompression)
        {
            switch (supportedCompression)
            {
                case HttpHeaderValues.DeflateCompression:
                    httpResponse.Filter = new DeflateStream(httpResponse.Filter, CompressionMode.Compress);
                    break;
                case HttpHeaderValues.GZipCompression:
                    httpResponse.Filter = new GZipStream(httpResponse.Filter, CompressionMode.Compress);
                    break;
            }

            httpResponse.AppendHeader(HttpHeaderValues.ContentEncodingHeader, supportedCompression);
            // this line is where i have the problem
            httpResponse.Cache.VaryByHeaders[HttpHeaderValues.AcceptEncodingHeader] = true;
        }
    }

我收到“无法将索引应用于 IHttpCacheVaryByHeaders 类型的表达式”错误。 我有响应和缓存的接口,但是如何在接口中表示 VaryByHeaders,然后在具体类中使用它?

【问题讨论】:

    标签: asp.net nunit httpresponse nsubstitute


    【解决方案1】:

    错误似乎表明IHttpCacheVaryByHeaders 没有声明索引器(例如bool this[string header] { get; set; }),但与其自己实现这些包装器,不如尝试HttpResponseWrapper 和其他System.Web.Abstractions。这将使测试这些东西变得容易得多。 :)

    【讨论】:

    • 嗨大卫非常感谢你,我正在使用 bool this[string header] { get;放; } 现在修复我的测试,但是当我有机会时会查看抽象,我不确定 bool this[string header] { get;放;有效,但我会调查它。干杯。
    猜你喜欢
    • 1970-01-01
    • 2014-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-29
    • 1970-01-01
    • 1970-01-01
    • 2010-11-03
    相关资源
    最近更新 更多