HttpContext和HttpContextWrapper在单元测试和Web窗体和MVC方面的区别

18 asp.net-mvc asp.net-mvc-4

我知道之间的差别HttpContext,并HttpContextWrapper低于...

的HttpContext

这是复古的asp.net上下文.这个问题是它没有基类而且不是虚拟的,因此不能用于测试(不能模拟它).建议不要将它作为函数参数传递,而是传递HttpContextBase类型的变量.

HttpContextBase

这是(新的c#3.5)替换HttpContext.由于它是抽象的,现在它是可以模仿的.我们的想法是,期望传递上下文的函数应该会收到其中一个.它由HttpContextWrapper具体实现

HttpContextWrapper

在C#3.5中也是新的 - 这是HttpContextBase的具体实现.要在普通网页中创建其中一个,请使用新的HttpContextWrapper(HttpContext.Current).

我们的想法是,为了使您的代码单元可测试,您将所有变量和函数参数声明为HttpContextBase类型,并使用IOC框架(例如Castle Windsor)来注入它.在普通代码中,castle将注入相当于'new HttpContextWrapper(HttpContext.Current)',而在测试代码中,您将获得一个HttpContextBase的模拟.

但我不知道它的实际用途.我听说在与Web窗体进行比较时,它在单元测试中很有用.但它有用吗?


我也知道我们可以用它来执行这里提到的控制器和Action

Dar*_*rov 19

我听说在与Web窗体进行比较时,它在单元测试中很有用.但它有用吗?

让我们举一个ASP.NET MVC控制器动作的例子,它在响应中添加一个cookie:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        var cookie = new HttpCookie("foo", "bar");
        this.Response.Cookies.Add(cookie);
        return View();
    }
}
Run Code Online (Sandbox Code Playgroud)

注意那里的Response属性.这是一个HttpResponseBase.所以我们可以在单元测试中模拟它:

public class HttpResponseMock: HttpResponseBase
{
    private HttpCookieCollection cookies;
    public override HttpCookieCollection Cookies
    {
        get
        {
            if (this.cookies == null)
            {
                this.cookies = new HttpCookieCollection();
            }

            return this.cookies;
        }
    }
}

public class HttpContextMock: HttpContextBase
{
    private HttpResponseBase response;

    public override HttpResponseBase Response
    {
        get 
        {
            if (this.response == null)
            {
                this.response = new HttpResponseMock();
            }
            return this.response;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

现在我们可以编写一个单元测试:

// arrange
var sut = new HomeController();
var httpContext = new HttpContextMock();
sut.ControllerContext = new ControllerContext(httpContext, new RouteData(), sut);

// act
var actual = sut.Index();

// assert
Assert.AreEqual("bar", sut.Response.Cookies["foo"].Value);
Run Code Online (Sandbox Code Playgroud)

由于所有成员都是虚拟的,我们可以使用一个模拟框架,这将避免我们需要为单元测试编写那些模拟类.例如,对于NSubstitute,测试的外观如下:

// arrange
var sut = new HomeController();
var context = Substitute.For<HttpContextBase>();
var response = Substitute.For<HttpResponseBase>();
var cookies = new HttpCookieCollection();
context.Response.Returns(response);
context.Response.Cookies.Returns(cookies);
sut.ControllerContext = new ControllerContext(context, new RouteData(), sut);

// act
var actual = sut.Index();

// assert
Assert.AreEqual("bar", sut.Response.Cookies["foo"].Value);
Run Code Online (Sandbox Code Playgroud)

现在让我们来看一个WebForm:

protected void Page_Load(object sender, EventArgs)
{
    var cookie = new HttpCookie("foo", "bar");
    this.Response.Cookies.Add(cookie);
}
Run Code Online (Sandbox Code Playgroud)

在这种情况下,Response属性是具体的HttpResponse.所以你被摧毁了.无法单独进行单元测试.