zoukankan      html  css  js  c++  java
  • Nop中的Fake类

    1.Nop提供了完整的FakeHttpContext实现,如图

    image

    1.FakeHttpContext的作用。

    控制器进行单元测试时模拟web上下文环境,还有一些其他对HttpContext有依赖的组件也可以使用Fake从而进行组件的单元测试。

    2.例举Nop中使用到FakeHttpContext的地方

    (1).NopEngine(初始化NopEngine时,是web环境就注册真实HttpContext,否则注册FakeHttpContext)

    //HTTP context and other related stuff
                builder.Register(c => 
                    //register FakeHttpContext when HttpContext is not available
                    HttpContext.Current != null ?
                    (new HttpContextWrapper(HttpContext.Current) as HttpContextBase) :
                    (new FakeHttpContext("~/") as HttpContextBase))
                    .As<HttpContextBase>()
                    .InstancePerHttpRequest();
                builder.Register(c => c.Resolve<HttpContextBase>().Request)
                    .As<HttpRequestBase>()
                    .InstancePerHttpRequest();
                builder.Register(c => c.Resolve<HttpContextBase>().Response)
                    .As<HttpResponseBase>()
                    .InstancePerHttpRequest();
                builder.Register(c => c.Resolve<HttpContextBase>().Server)
                    .As<HttpServerUtilityBase>()
                    .InstancePerHttpRequest();
                builder.Register(c => c.Resolve<HttpContextBase>().Session)
                    .As<HttpSessionStateBase>()
                    .InstancePerHttpRequest();

    (2)RoutesTests( Route依赖HttpContextBase,可以用Fake类代替 )

    [Test]
            public void Blog_routes()
            {
                //TODO why does it pass null instead of "new BlogPagingFilteringModel()" as it's done in real application? The same is about issue is in the other route test methods
                "~/blog/".ShouldMapTo<BlogController>(c => c.List(null));
                "~/blog/rss/1".ShouldMapTo<BlogController>(c => c.ListRss(1));
                "~/blog/2/".ShouldMapTo<BlogController>(c => c.BlogPost(2));
                "~/blog/2/test-se-name".ShouldMapTo<BlogController>(c => c.BlogPost(2));
                //TODO validate properties such as 'Tag' or 'Month' in the passed BlogPagingFilteringModel. The same is about issue is in the other route test methods
                //"~/blog/tag/sometag".ShouldMapTo<BlogController>(c => c.List(new BlogPagingFilteringModel() { Tag = "sometag" }));
                //"~/blog/month/3".ShouldMapTo<BlogController>(c => c.List(new BlogPagingFilteringModel() { Month = "4" }));
            }

    (3)其他组件,如WebHelperTests,AdminAuthorizeAttributeTests都依赖了HttpContextBase

    总结:这些Fake类为单元测试提供了模拟的Web上下文环境。,再也不用担心控制器以及类库因为用了HttpContextBase(最常见的包括Session,Request,Response)而变得难以进行单元测试了(但在控制器中还是应该尽量少用这些)

  • 相关阅读:
    【BZOJ】2019: [Usaco2009 Nov]找工作(spfa)
    【BZOJ】3668: [Noi2014]起床困难综合症(暴力)
    Redis 字符串结构和常用命令
    Redis实现存取数据+数据存取
    Spring 使用RedisTemplate操作Redis
    使用 java替换web项目的web.xml
    SQL server 从创建数据库到查询数据的简单操作
    SQL server 安装教程
    IntelliJ IDEA 注册码(因为之前的地址被封杀了,所以换了个地址)
    对程序员有帮助的几个非技术实用链接(点我,你不会后悔的)
  • 原文地址:https://www.cnblogs.com/miku/p/2699413.html
Copyright © 2011-2022 走看看