csharp Asp.Net单元测试

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp Asp.Net单元测试相关的知识,希望对你有一定的参考价值。

/// <summary>
/// Mvc Context 工厂
/// </summary>
public static class MvcContextMockFactory
{
    #region ControllerContext
    private static ControllerContext controllerContext = null;
    /// <summary>
    /// 创建ControllerContext
    /// </summary>
    /// <param name="controller">Controller</param>
    /// <param name="contextBase">httpContextBase</param>
    /// <returns></returns>
    public static ControllerContext CreateControllerContext(Controller controller, HttpContextBase contextBase)
    {
        controllerContext = new ControllerContext
            (
            contextBase,
            new RouteData(),
            controller);
        return controllerContext;
    }

    /// <summary>
    /// 创建ControllerContext
    /// </summary>
    /// <param name="controller">controller</param>
    /// <param name="url">访问的url地址</param>
    /// <param name="httpMethod">访问的方法(get,post)</param>
    /// <param name="name">路由名称</param>
    /// <param name="pattern">路由格式</param>
    /// <param name="obj">路由默认值</param>
    /// <returns></returns>
    public static ControllerContext CreateControllerContext
        (Controller controller, string url, string httpMethod, string name, string pattern, string obj)
    {
        controllerContext = new ControllerContext
            (
            CreateHttpContext(),
            GetRouteData(url, httpMethod, name, pattern, obj),
            controller);
        return controllerContext;
    }

    /// <summary>
    /// 创建ControllerContext
    /// </summary>
    /// <param name="controller">controller</param>
    /// <param name="contextBase">HttpContextBase</param>
    /// <param name="url">访问的url地址</param>
    /// <param name="httpMethod">访问的方法(get,post)</param>
    /// <param name="name">路由名称</param>
    /// <param name="pattern">路由格式</param>
    /// <param name="obj">路由默认值</param>
    /// <returns></returns>
    public static ControllerContext CreateControllerContext
        (Controller controller, HttpContextBase contextBase, string url, string httpMethod, string name, string pattern, string obj)
    {
        controllerContext = new ControllerContext
            (
            contextBase,
            GetRouteData(url, httpMethod, name, pattern, obj),
            controller);
        return controllerContext;
    }

    /// <summary>
    /// 创建ControllerContext
    /// </summary>
    /// <param name="controller">Controller</param>
    /// <returns></returns>
    public static ControllerContext CreateControllerContext(Controller controller)
    {
        controllerContext = new ControllerContext
            (
            CreateHttpContext(),
            new RouteData(),
            controller);
        return controllerContext;
    }


    #endregion
    #region HttpContextBase
    /// <summary>
    /// 创建HttpContextBase
    /// </summary>
    /// <returns></returns>
    public static HttpContextBase CreateHttpContext(
        Mock<HttpContextBase> context = null
        , Mock<HttpRequestBase> request = null
        , Mock<HttpResponseBase> response = null
        , Mock<HttpSessionStateBase> session = null
        , Mock<HttpServerUtilityBase> server = null)
    {
        context = context ?? new Mock<HttpContextBase>();
        request = request ?? new Mock<HttpRequestBase>();
        response = response ?? new Mock<HttpResponseBase>();
        session = session ?? new Mock<HttpSessionStateBase>();
        server = server ?? new Mock<HttpServerUtilityBase>();
        response
            .Setup(rsp => rsp.ApplyAppPathModifier(Moq.It.IsAny<string>()))
            .Returns((string s) => s);

        context.Setup(ctx => ctx.Request).Returns(request.Object);
        context.Setup(ctx => ctx.Response).Returns(response.Object);
        context.Setup(ctx => ctx.Session).Returns(session.Object);
        context.Setup(ctx => ctx.Server).Returns(server.Object);

        return context.Object;
    }


    #endregion


    #region Private Method
    private static HttpContextBase CreateHttpContext(string url, string httpMethod)
    {
        var context = new Mock<HttpContextBase>();
        var request = new Mock<HttpRequestBase>();
        var response = new Mock<HttpResponseBase>();
        var session = new Mock<HttpSessionStateBase>();
        var server = new Mock<HttpServerUtilityBase>();
        response
            .Setup(rsp => rsp.ApplyAppPathModifier(Moq.It.IsAny<string>()))
            .Returns((string s) => s);

        request.Setup(req => req.AppRelativeCurrentExecutionFilePath).Returns(url);
        request.Setup(req => req.HttpMethod).Returns(httpMethod);

        context.Setup(ctx => ctx.Request).Returns(request.Object);
        context.Setup(ctx => ctx.Response).Returns(response.Object);
        context.Setup(ctx => ctx.Session).Returns(session.Object);
        context.Setup(ctx => ctx.Server).Returns(server.Object);

        return context.Object;
    }

    private static RouteData GetRouteData(string url, string httpMethod, string name, string pattern, string obj)
    {
        RouteTable.Routes.MapRoute(name, pattern, obj);
        var routeData =
            RouteTable.Routes.
            GetRouteData(CreateHttpContext(url, httpMethod));
        return routeData;
    }
    #endregion

}

以上是关于csharp Asp.Net单元测试的主要内容,如果未能解决你的问题,请参考以下文章

csharp ASP.NET的助手,ASP.NET MVC应用服务器端测试

ASP.NET 系列:单元测试

ASP.NET 系列:单元测试

ASP.NET 系列:单元测试之SmtpClient

ASP.NET 系列:单元测试之ConfigurationManager

单元测试 ASP.NET Web API